Problems for producers and consumers in Java

Source: Internet
Author: User

1----Replace synchronized with the lock interface in the Java.util.concurrent.locks package, by Reentrantlock the class that has implemented the lock interface,

Create the Reentrantlock object Lock (private final reentrantlock lock = new Reentrantlock ()), and let the thread get the lock by using the Lock () method in this object (Lock.lock () ;);

Use Unlock (), Release a Lock (Lock.unlock ();) when exiting a block of code,

2----where the while flag resolves the duplicate production of if flag, causing the consumer to lose the read of the data being overwritten (roast chicken). But while the while causes a deadlock, all threads are dormant, which can be notify () Modification to Notifyall () causes the program to wake up all the time, and the indirect implementation wakes up to each other, but because there is no point in waking the same thread, the program's efficiency is reduced.

3---Solve the previous problem by using the condition interface in Java.util.concurrent.locks, by creating different condition instances of the same lock, and invoking their respective await () and signal with different instances ( ) method to implement freeze and wake Management for t0 t1 and T2 T3 2 sets of threads. This eliminates the need to wake up every time with a reduced efficiency problem.

1 /*2 demand: Producer----> Consumer3 resources: Roast Duck one4 every production of one, on the consumption of a Zhi5 */6 Importjava.util.concurrent.locks.*;7 classResource8 {9     PrivateString name;Ten     Private intCount=0; One     Private Booleanflag=false; A     Private FinalReentrantlock lock =NewReentrantlock (); -     FinalCondition Cond1 =lock.newcondition (); -     FinalCondition Cond2 =lock.newcondition (); the      Public  voidMake (String name) -     { - Lock.lock (); -          while(flag) +             Try -             { + cond1.await (); A             } at             Catch(interruptedexception e) -             { -             } -              -          This. Name =name; -count++; inflag=true; - cond2.signal (); toSystem.out.println (Thread.CurrentThread (). GetName () + "producer ..." +name+count); + Lock.unlock (); -     } the      Public voidout () *     { $ Lock.lock ();Panax Notoginseng          while(!flag) -             Try the             { + cond2.await (); A             } the             Catch(interruptedexception e) +             { -             } $System.out.println (Thread.CurrentThread (). GetName () + "consumer ..." +count); $Flag =false; - cond1.signal (); - Lock.unlock (); the     } -     Wuyi } the classProducerImplementsRunnable - { WuResource r =NewResource (); - Producer (Resource R) About     { $          This. R =R; -     } -      Public voidRun () -     { A          while(true) +R.make ("Roast Duck"); the     } - } $ classCustomerImplementsRunnable the { theResource r =NewResource (); the Customer (Resource R) the     { -          This. R =R; in     } the      Public voidRun () the     { About          while(true) the r.out (); the     } the } + classProducercustomer - { the      Public Static voidMain (string[] args)Bayi     { the         //Create a resource class theResource r =NewResource (); -         //Create a Task object -Producer Pro =NewProducer (r); theCustomer cus =NewCustomer (r); the         //Build Thread theThread T0 =NewThread (pro); theThread T1 =NewThread (pro); -Thread t2 =NewThread (cus); theThread t3 =NewThread (cus); the         //Open Thread the T0.start ();94 T1.start (); the T2.start (); the T3.start (); the 98     } About}

Problems for producers and consumers in Java

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.