A case study of lock+condition relative to Wait+notify

Source: Internet
Author: User

Description of the problem

Starting 3 threads prints an incremented number, thread 1 prints 1,2,3,4,5 first, then thread 2 prints 6,7,8,9,10, and then thread 3 prints 11,12,13,14,15. Then the thread 1 prints 16,17,18,19,20 .... And so on, until you print to 45.

Wait+notify implementation:

Package Com.tonyluis;public class Numberprintdemo {static int n = 1;static int state = 0;public static void Main (string[] args) {new Thread (new MyThread (0)). Start (); New Thread (New MyThread (1)). Start (); New Thread (New MyThread (2)). Start ();} Class MyThread implements runnable{private int state; MyThread () {this (0);} MyThread (int state) {this.state=state;} public void Run () {String threadname=thread.currentthread (). GetName (), for (int i = 0; i < 3; i++) {synchronized (Mythre Ad.class) {while (state = numberprintdemo.state) try {MyThread.class.wait ();} catch (Interruptedexception e) { E.printstacktrace ();} for (int j = 0; J < 5; j + +) System.out.println (threadname+ ":" + numberprintdemo.n++); System.out.println (); numberprintdemo.state++; numberprintdemo.state%=3; MyThread.class.notifyAll ();}}}

Lock+condition implementation:

Package Com.tonyluis;import java.util.concurrent.locks.*;p ublic class Numberprint {static int state = 0;static int n = 1;s  Tatic Lock lock = new Reentrantlock (), Static Condition condition[]=new condition[3];p ublic static void Main (string[] args) {for (int i=0;i<condition.length;i++) condition[i]=lock.newcondition (); New Thread (New MyThread (0)). Start (); new Thread (new MyThread (1)). Start (); New Thread (New MyThread (2)). Start ();}} Class MyThread implements runnable{private int state; MyThread () {this (0);} MyThread (int state) {this.state=state;} public void Run () {String threadname=thread.currentthread (). GetName (), for (int i = 0; i < 5; i++) {try {Numberprint.loc K.lock (); while (state = numberprint.state) try {numberprint.condition[state].await ();} catch (Interruptedexception e) {E.printstacktrace ();} for (int j = 0; J < 5; j + +) System.out.println (threadname+ ":" + numberprint.n++); System.out.println (); numberprint.state++; Numberprint.state%=numberprint.condition.length; Numberprint.condition[numbErprint.state].signal ();} finally {NumberPrint.lock.unlock ();}}}}

With the implementation of Wait+notify, after thread 0 enters the Notifyall () method, threads 1 and 2 are awakened, thread 1 and thread 2, and thread 0 will compete, although eventually the thread 1 gets the locks, the process may be more tortuous.

Using the Lock+condition implementation method, thread 0 only wakes up thread 1, so that only thread 1 and thread 0 compete, more precisely, especially if the concurrency is relatively large.

A case study of lock+condition relative to Wait+notify

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.