public class Sync implements Runnable{ ? ?int b = 100; ? ?public synchronized void m1() throws Exception { ? ? ? ?b = 1000; ? ? ? ?Thread.sleep(3000); ? ? ? ?System.out.println("sync " + b); ? ?} ? ?public void m2() { ? ? ? ?System.out.println(b); ? ?} ? ?public void run() { ? ? ? ?try { ? ? ? ? ? ?m1(); ? ? ? ?} catch (Exception e) { ? ? ? ? ? ?e.printStackTrace(); ? ? ? ?} ? ?} ? ?public static void main(String[] args) throws InterruptedException { ? ? ? ?Sync s = new Sync(); ? ? ? ?Thread t = new Thread(s); ? ? ? ?t.start(); ? ? ? ?Thread.sleep(3000); ? ? ? ?s.m2(); ? ?}}這個程序為什么輸出的是下面這個啊?m2方法輸出的應該是100啊,把b賦值為1000的語句被synchronized鎖定了,其他對象明明是不能訪問的啊1000sync 1000
添加回答
舉報
0/150
提交
取消