3, traditional thread synchronization and communication--production and consumption examples

Source: Internet
Author: User

Core points:

1, the lock object must be the same. the 2, Wait (), and notify () methods must be methods that invoke the lock object, not this (thread). 3, in the multi-production and more consumption of attention to use Notifyall instead of notifyall, otherwise it will cause deadlock

Test code:

1 Importjava.util.LinkedList;2 ImportJava.util.Queue;3 ImportJava.util.Random;4 5 /**6 * Multiple production-consumer thread synchronous communication7 * Core points:8 * 1, the lock object must be the same. 9 * 2, wait (), and notify () methods must be methods that call the lock object, not this (thread). Ten * 3, in the multi-production and more consumption when the attention to use Notifyall instead of Notifyall One  * @authorYzl A  * -  */ -  Public classThreadpart_3 { the      Public Static voidMain (string[] args) { - test (); -     } -      Public Static voidTest () { +         FinalFactory Factory =NewFactory (); -Thread Shengchan1 =NewThread (NewRunnable () { + @Override A              Public voidrun () { at                  while(true){ - Factory.add (); -                 } -             } -         }); - Shengchan1.start (); inThread shengchan2 =NewThread (NewRunnable () { - @Override to              Public voidrun () { +                  while(true){ - Factory.add (); the                 } *             } $         });Panax Notoginseng Shengchan2.start (); -          theThread XIAOFEI1 =NewThread (NewRunnable () { + @Override A              Public voidrun () { the                  while(true){ + factory.sub (); -  $                 } $             } -         }); - Xiaofei1.start (); theThread Xiaofei2 =NewThread (NewRunnable () { - @OverrideWuyi              Public voidrun () { the                  while(true){ - factory.sub (); Wu                 } -             } About         }); $ Xiaofei2.start (); -     } - } -  A classfactory{ +     Privatequeue<integer> queue =NewLinkedlist<integer>(); the     //Production -      Public synchronized voidAdd () { $         //use while instead of if to prevent false wakeup the          while(Queue.size () > 0){ the             Try { the                  This. Wait (); the}Catch(interruptedexception e) { - e.printstacktrace (); in             } the         } theInteger val =NewRandom (). Nextint (100); About Queue.add (val); theSystem.out.println ("Producer:" + Thread.CurrentThread (). GetName () + "add:" +val); the          This. Notifyall (); the         Try { +Thread.Sleep (1000); -}Catch(interruptedexception e) { the e.printstacktrace ();Bayi         } the     } the     //Consumer -      Public synchronized voidSub () { -         //use while instead of if to prevent false wakeup the          while(queue.size () = = 0 ){ the             Try { the                  This. Wait (); the}Catch(interruptedexception e) { - e.printstacktrace (); the             } the         } theInteger val =Queue.poll ();94System.out.println ("Consumer:" + thread.currentthread (). GetName () + "Get:" +val); the          This. Notifyall (); the         Try { theThread.Sleep (1000);98}Catch(interruptedexception e) { About e.printstacktrace (); -         }101     }102}

Examples of running results:

1Producer: Thread-0 Add:122Consumer: Thread-3 Get:123Producer: Thread-1 add:844Consumer: Thread-2 get:845Producer: Thread-0 add:286Consumer: Thread-3 get:287Producer: Thread-1 add:608Consumer: Thread-2 get:609Producer: Thread-0 add:71TenConsumer: Thread-3 get:71 OneProducer: Thread-1 add:63 AConsumer: Thread-3 get:63 -Producer: Thread-0 Add:3 -Consumer: Thread-2 Get:3 theProducer: Thread-1 add:62 -Consumer: Thread-3 get:62 -Producer: Thread-0 add:90 -Consumer: Thread-2 get:90 +Producer: Thread-1 Add:9 -Consumer: Thread-3 Get:9

3, traditional thread synchronization and communication--production and consumption examples

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.