Multi-threaded wait wakeup (using monitor)

Source: Internet
Author: User

/* After the jdk1.5 version, there are some new features that have improved the threading of the principle. An interface lock is provided in the Java.util.concurrent.locks package. replaced the synchronized. Synchronized The use of a lock operation is implicit. Lock interface, which is displayed with the locking operation. Done by two methods: Lock (): Gets the lock. Unlock (): Release the lock. There is another object, Condition. The appearance of the object replaces the wait notify in object Notifyall the methods of these operations monitors. Alternative way: await signal signalall. Next, replace the following code with a new object that has only the JDK1.5 version. The biggest benefit of the new feature is that you can add multiple sets of monitor objects in a lock lock.  This will enable the party to only wake the other side of the thread lock, is the mechanism of synchronization. Control synchronization through locks. The monitor is the operation used to synchronize the objects in. Like Wait,notify. Notifyall. Each group of monitor methods corresponds to a lock. After jdk1.5, the way of the monitor from the object, encapsulated into the condition objects, each lock lock, can correspond to multiple sets of monitor objects, which can be implemented only to wake the other side of the operation. */import java.util.concurrent.locks.*;class res{private String name;private int count = 0;private Boolean B = false;//defines a A lock. Lock lock = new Reentrantlock ();//through the specified lock, a monitor object can be used to create one of the locks. Condition ProCon = Lock.newcondition ();//The upgraded lock can correspond to multiple sets of monitor objects. Condition Cuscon = lock.newcondition ();p ublic void Set (String name) {//Get lock. Lock.lock (); Try{while (b) procon.await (); this.name = name+ "--------" +count;count++; System.out.println (Thread.CurrentThread (). GetName () + "..... Producer .... "+this.name); b = True;cuscon.signal ();} catch (Interruptedexception e) {}finally{//release lock Lock.unlock ();}} public void Out () {lock.lock (); Try{while (!b) cuscon.await (); System.out.println (Thread.CurrentThread (). GetName () + "----Consumer---" +this.name); b = false;procon.signal ();} catch (Interruptedexception e) {}finally{lock.unlock ();}}} Class Pro implements Runnable{private Res R; Pro (Res r) {THIS.R = R;} public void Run () {while (true) {R.set ("Product");}}} Class Cus implements Runnable{private Res R; Cus (Res r) {THIS.R = R;} public void Run () {while (true) {R.out ()}}} Class ProCusDemo2 {public static void main (string[] args) {res r = new Res (); Pro p = new Pro (R); Cus C = new Cus (r); thread T1 = new Thread (p); Thread t2 = new Thread (p); thread t3 = new thread (c); thread T4 = new thread (c);//t1,t2 are all producers. T3,T4 are all consumers. T1.start (); T2.start (); T3.start (); T4.start ();}}

  

Multi-threaded wait wakeup (using monitor)

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.