public class ClosureTest
{
public static void main(String[] args)
{
//定义一个局部变量
final String str = "Java";
//在内部类里访问局部变量str
new Thread(new Runnable()
{
public void run()
{
for (int i = 0; i < 100 ; i++ )
{
//此处将一直可以访问到str局部变量
System.out.println(str + " " + i);
//暂停0.1秒
try
{
Thread.sleep(100);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
}).start();
//执行到此处,main方法结束
for (int i = -1; i > -100 ; i-- )
{
System.out.println(str + " " + i);
}
}
}