代码在此,他说F1和F2都被synchronized了,虽然先f1.start().但是后面跟着执行F2();
那么必须等F2执行完了,才能执行F1。
我就不懂了,F1在F2之前开始执行,为什么不是先锁住F1然后等F1执行完以后再执行F2呢?
- Java code
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); } }
------解决方案--------------------
------解决方案--------------------
多线程那里
------解决方案--------------------
- Java code
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); }