代码如下:
public class TT implements Runnable{
int num=1;
public void run() {
try {
System.out.println("名字"+Thread.currentThread().getName());
num=1000;
System.out.println("num="+num);
//m1();
} catch (Exception e) {
e.printStackTrace();
}
}
public void m2(){
System.out.println(num);
}
public static void main(String[] args) throws Exception {
TT tt=new TT();
Thread thread=new Thread(tt);
thread.start();
tt.m2();
}
}
为什么输出的结果 每次都是
1
名字Thread-0
num=1000
上面的代码有 main 主线程 然后我又创建了线程thread 启动了线程 应该由CPU来分配随机 先执行run()和m2
为什么每次都是先执行 tt.m2()呢?
问题2
public synchronized void run(){} 和synchronized public void run(){} synchronized位置不一样 有区别吗? |
|