那位大侠帮忙讲一下java中多线程变量传递的问题?class MyTimer extends Thread { static int count = 0;
public void run()
{
count +=4;
}
}
public class Test
{
public static void main(String args[])
{
new MyTimer().start();
System.out.println(" " + MyTimer.count);
}
}---------- run ----------
0输出完成 (耗时: 0 秒)为什么执行start()之后还是初始值?
public void run()
{
count +=4;
}
}
public class Test
{
public static void main(String args[])
{
new MyTimer().start();
System.out.println(" " + MyTimer.count);
}
}---------- run ----------
0输出完成 (耗时: 0 秒)为什么执行start()之后还是初始值?
waitMyTimerTerminated();System.out.println(" " + MyTimer.count);
线程的执行是随机的,有系统来分配CPU时间,所以不是start了就表示线程就一定能获得CPU时间
new MyTimer().start();线程就绪状态,没来得及运行,主线程就已经开始了。
new MyTimer().start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" " + MyTimer.count);