日期:2014-05-20 浏览次数:20739 次
public class TestSync2 implements Runnable{ static int b = 10; public synchronized void f1(){ try{ Thread.sleep(1000); } catch (InterruptedException ie){} int b = 1000; //synchronized (f2); System.out.println("B1 = " + b); } public synchronized static void f2(){ try{ Thread.sleep(5000); } catch (InterruptedException ie){} int b = 2000; System.out.println("B2 = " + b); } public void run(){ f1(); } public static void main(String[] args){ TestSync2 ts = new TestSync2(); Thread tst = new Thread(ts); tst.start(); TestSync2.f2(); System.out.println("ALL DONE B= " + ts.b); } }
public synchronized static void f2(){ try{ [color=#FF0000]System.out.printl("运行f2");[/color] Thread.sleep(5000); } catch (InterruptedException ie){} int b = 2000; System.out.println("B2 = " + b); }