Java threads Live and thread execution issues!

Source: Internet
Author: User

1 /*2 The following program will appear below, when Thread-0, Thread-1, Thread-2 are wait, they may wake up at the same time3 Thread-0 put4 Thread-1 put5 Thread-2 put6 Thread-3 get//Here, Thread-3 get the lock, wake up all the waiting threads and have the following output7 Thread-2 put8 Thread-1 put9 Thread-0 putTen */ One  A  - Although multiple threads will wake up at the same time, only one can get the CPU execution right!  - in short, the synchronization can only be performed in one, but the survival is not necessarily one!  the  -  - classduckd{ -      Public voidput () { +          -Synchronized (DUCKD.class){ +            ASystem. out. println (Thread.CurrentThread (). GetName () +"put"); at               Try{ -DUCKD.class. Wait ();//Thread-0, Thread-1, Thread-2 may wake up here at the same time!  -}Catch(interruptedexception e) { -                   -               } -               //........ in         } -     } to      +      Public void Get(){ -          theSynchronized (DUCKD.class){ *  $DUCKD.class. Notifyall ();Panax NotoginsengSystem. out. println (Thread.CurrentThread (). GetName () +"Get"); -               Try{ theDUCKD.class. Wait (); +}Catch(interruptedexception e) { A                   the               }          +         } -     } $ } $  - classproduced implements Runnable{ - duckd dk; the produced (Duckd dk) { -         This. dk=dk;Wuyi     } the      Public voidrun () { -         while(true) Wu dk.put (); -     }     About } $  - classconsumed implements runnable{ - duckd dk; - consumed (duckd dk) { A         This. dk=dk; +     } the      Public voidrun () { -          while(true) $Dk.Get(); the     }     the } the  the classtest{ -     Public Static voidMain (string[] args) { inDUCKD dk=Newduckd (); theThread t1=NewThread (Newproduced (DK)); theThread t2=NewThread (Newproduced (DK)); AboutThread t3=NewThread (Newproduced (DK)); theThread t4=NewThread (Newconsumed (DK)); the         the T1.start (); + T2.start (); - T3.start (); the T4.start ();Bayi    } the}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.