62,614
社区成员
发帖
与我相关
我的任务
分享
public class Mythread03 implements Runnable{
int b = 100;
public synchronized void m1(){
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
b = 1000;
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("b="+b);
}
public synchronized void m2(){
try {
Thread.sleep(4000);
b = 2000;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run(){
m1();
}
public static void main(String[] args) {
Mythread03 th = new Mythread03();
Thread t = new Thread(th);
t.start();
try { //不加这个主线程的m2比m1先执行????不太懂
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
th.m2();
System.out.println(th.b);
}
}