public class FinalTest {public static void main(String[] args) {new Fina..._局部多线程">
赞
踩
文章标题看起来是一个错误命题,因为在java的内存模型中方法的局部变量是放在线程私有的栈里的。下图是java虚拟机的内存模型:
?
那么如何访问呢?这当然是有前提的,首先必须是内部类,局部变量必须加final修饰符。看代码示例:
class="java">public class FinalTest {
public static void main(String[] args) {
new FinalTest().test();
Thread t = Thread.currentThread();
System.out.println("Thread" + t.getId() + ":主线程结束");
// 执行到此处,主线程结束
}
public void test() {
// 定义一个局部变量
final int var = 1;
final int[] refer = {1};
// 在内部类里访问局部变量var和refer
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 10; i++) {
// 此处将一直可以访问到var局部变量
Thread t = Thread.currentThread();
System.out.println("Thread" + t.getId() + ":" + (var + i));//实际上编译后是System.out.println(1 + i)
//引用类型局部变量
System.out.println("Thread" + t.getId() + ":" + refer +"="+ (refer[0] + i));
// 暂停0.1秒
try {
Thread.sleep(100);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}).start();
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 5; i++) {
//var = var + i;//不能修改
//引用类型虽然不让修改变量的引用地址,但是可以修改变量里的内容
//refer = {i};//不允许
refer[0] = i;//允许
// 暂停0.2秒
try {
Thread.sleep(200);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}).start();
System.gc();//主线程执行完毕后,通知虚拟机垃圾回收,此时无论线程执行多久,var和refer都不会被回收掉
}
}
?不启动第二个线程时运行结果如下:
?
Thread1:主线程结束
Thread8:1
Thread8:[I@69b332=1
Thread8:2
Thread8:[I@69b332=2
Thread8:3
Thread8:[I@69b332=3
Thread8:4
Thread8:[I@69b332=4
Thread8:5
Thread8:[I@69b332=5
Thread8:6
Thread8:[I@69b332=6
Thread8:7
Thread8:[I@69b332=7
Thread8:8
Thread8:[I@69b332=8
Thread8:9
Thread8:[I@69b332=9
Thread8:10
Thread8:[I@69b332=10
??启动第二个线程时运行结果如下:
?
Thread8:1
Thread1:主线程结束
Thread8:[I@173a10f=0
Thread8:2
Thread8:[I@173a10f=1
Thread8:3
Thread8:[I@173a10f=3
Thread8:4
Thread8:[I@173a10f=4
Thread8:5
Thread8:[I@173a10f=5
Thread8:6
Thread8:[I@173a10f=7
Thread8:7
Thread8:[I@173a10f=8
Thread8:8
Thread8:[I@173a10f=10
Thread8:9
Thread8:[I@173a10f=11
Thread8:10
Thread8:[I@173a10f=13
?
?
大小: 54.1 KB
查看图片附件
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。