Public synchronized void (m1) throws the Exception { B=1000; Thread.sleep (5000); System.out.println("b=" + b); }
Public synchronized void m2 () throws the Exception { Thread.sleep (2500); B=2000; }
Public void the run () { Try { (m1); } the catch (Exception e) { e.printStackTrace(); } }
Public static void main (String [] args) throws the Exception { TT TT=new (TT); Thread t=new Thread (tt); t.start(); Tt. M2 (); System. The out. Println (tt. B); } [code=Java]}
[/code] Want to ask why the t.s tart (where) on the thread doesn't lock the execution m1 () but to perform in the main function of tt. M2 m2 () () and then lock the implementation,
CodePudding user response:
Start does not guarantee that the thread will be able to start immediately (thread startup is random) You can be in tt. Additional Thread before m2. Sleep to the main line and so on for a while, to ensure that t start to call again after tt. M2