为什么结果为:
2000
a =1000
b = 1000
竟然不是m1()先执行完、打印,而是主线程抢了先??- package org.qyx.online;
- public class TestSynchro implements Runnable {
- int b = 100;
- public synchronized void m1() throws Exception {
- b = 1000;
- Thread.sleep(1000);
- System.out.println("b = " + b);
- }
- public synchronized void m2() throws Exception {
- Thread.sleep(2500);
- b = 2000;
- System.out.println(b);
- }
- public void run() {
- try {
- m1();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) throws Exception {
- TestSynchro tt = new TestSynchro();
- Thread t = new Thread(tt);
- t.start();
- tt.m2();
- System.out.println("a =" + tt.b);
- }
- }
复制代码 |
|