OS

xiaoxiao2021-03-06  71

Wait, Notify, Sleep, Join These methods are common, here involve multi-threaded and synchronous problems, here is slightly explained. 1.Wait and Notify These two methods are methods in object, put it in one because they The relationship is very close. Wait is the synchronous lock waiting for this object, but call this method must first get the synchronous lock of this object, this a lot of people are dizzy. Let's explain these two methods first, then give a small Example Description. WAIT: Wait for the synchronous lock, you need to get the synchronization lock of the object, you can call this method, otherwise you receive an IllegalMonitorstateException, this is an exception of runtime. After calling this method, give up this synchronous lock. If you don't have a parameter, you only wait for others to wake up. If you bring a parameter, you will set the longest time. After this time, even if you wake up this thread, you will not wait. Notify: Wake up waiting for the object Synchronous lock thread (only one, if there are multiple waiting), but NotifyAll can awaken all waiting threads, pay attention to WAIT WAIT before Notify, there is no effect. An example of a popular example, two There is a bathroom with a total of one toilet (only one person can be used each time), they all have to brush their teeth and convenient, they are trying, turns, first person brush your teeth, then brush your teeth ... Class syn {public static Void main (string [] args) throws exception {twovern (); twovern.tart (); twovern.tart (); 2;}} Class TwoteOpe Extends thread {private int i = 0; static thread one = new twoteople (1); Static thread two = new twovern (2); static object washroom = new object (); private warople (int i) {this.i = i;} public void Run () {synchronized (Washroom) {try {if (i = = 1) { Brush (); // 1 Washroom.wait (); // 2 release (); //6 Washroom.Notify (); //7} else {brush (); // 3 Washroom.notify (); // 4 Washroom.wait (); //5 Release (); // 8}} catch (interruptedException e) {E.PrintStackTrace ();}}} private void brush ()}} private void brush ()}} private void brush ()}} i "is brushing!"); try {thread.sleep (2000);} catch (interruptedException e) {E.PrintStackTrace ();} // delay two seconds to see effect system.out.println ("people" i " HAS BURSHED! ");

} Private void release () {system.out.println ("people" i "is release!"); Try {thread.sleep (2000);} catch (interruptedExcection E) {E.PrintStackTrace ();} // Delayed two seconds to see the effect system.out.println ("people" i "HAS RELEASED!");}}; The above code is short, but also shows the execution order, as for Join and Sleep or next discussion. Yesterday There is no time to finish this, and today, only two ways of Wait and Notify, discuss Sleep and Join, saying that these two methods are more simple than Wait and Notify. Http: // blog .9cbs.net / Treeroot / Archive / 2004/11/10 / 175508.ASPX Sleep: Thread static method, current thread sleep for a while, time to restore can be running, time is not necessarily executed, but also compete CPU. Join: This method is actually a special wait. Wait method usually requires someone NOTIFY (of course, it can also set up timeout), but the Join method does not require someone NOTIFY, and I have been waiting until this thread is temporary. Tell those who are waiting for it: You come in!) I am not very will give an example, or two people public a bathroom, this time does not brush your teeth, change the bath, you can't take a bath at the same time! Even if you can, It is not possible here. When this is like this: A is taken, B is waiting.

The first case: B is very smart, a bath may take 20 minutes to 1 hour, I will sleep 10 minutes to see if it is good, no better, sleep for 10 minutes, one more 10 minutes. Class syn { Public static void main (string [] args) throws exception {thread a = new bath (); a.start (); // b int Time = 0; while (a.isalive ()) {thread.sleep (10000) Time = 10; System.out.Println ("B Has WaITED" Time "Minutes");} System.out.Println ("B CAN Bath Now!");}}}}} Class bathing extends thread {public void Run ) {Zwide void bath () {system.out.println ("a is bathing!"); Try {thread.sleep (20000);} catch (interruptedexception e) {E.PrintStackTrace ();} // Delayed 20 seconds to see the effect system.out.println ("a Has Bathed!");}}; Here is not needed, but B may take more time, because it may just go to the door A. After washing, then he went to sleep again. The result was finished. The second situation: B became more smart, so I didn't lose, if I 10 minutes knock a door, I may have to wait 10 minutes, but if I per second I can't sleep once, so I thought of a trick, installed a agency, when A came out, the organ will pike the doorbell, so B can be high enough.

转载请注明原文地址:https://www.9cbs.com/read-92165.html

New Post(0)