那位大侠帮忙讲一下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()之后还是初始值?

解决方案 »

  1.   

    new MyTimer().start();//加一个等待函数
    waitMyTimerTerminated();System.out.println(" " + MyTimer.count);
      

  2.   

    又没人告诉你start之后要立马执行里面的run,它可以先把main继续往下执行也就是说,你打印的时候run方法还没执行呢
      

  3.   

    因为线程还没来得及执行,主线程就结束了
    线程的执行是随机的,有系统来分配CPU时间,所以不是start了就表示线程就一定能获得CPU时间
      

  4.   


    new MyTimer().start();线程就绪状态,没来得及运行,主线程就已经开始了。
    new MyTimer().start();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println(" " + MyTimer.count);