【发布时间】:2015-07-13 20:11:27
【问题描述】:
我无法理解,如果我的变量既是易失的又是静态的,那么为什么线程没有反映输出中的公共共享值 最后几行的输出是: 线程正在运行 4998线程-0 线程正在运行 4999线程-0 线程正在运行 4899线程-1 线程正在运行
public class Test implements Runnable{
volatile static int i=0;
@Override
public void run() {
for(;i<5000;i++)
{ try {
Thread t = Thread.currentThread();
String name = t.getName();
// Thread.sleep(10);
System.out.println(i+name);
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("Thread is running");
}}
public static void main(String[] args) {
Test t=new Test();
Thread t1=new Thread(t);
Thread t2=new Thread(t);
t1.start();
// t.run();
t2.start();
}
}
【问题讨论】:
标签: java multithreading