Java threads use the displayed lock and condition

Source: Internet
Author: User


Package Org.rui.thread.block2;import Java.util.concurrent.executorservice;import java.util.concurrent.Executors; Import Java.util.concurrent.timeunit;import Java.util.concurrent.locks.condition;import Java.util.concurrent.locks.lock;import java.util.concurrent.locks.reentrantlock;/** * Use the displayed Lock and condition, * Only in the more difficult multi-thread problem is required to use * * @author Lenovo * *///Wax Wax Electrical automatic Way public class WaxOMatic2 {public static void main (string[] Arg s) throws Interruptedexception {car car = new Car (); Executorservice exec = Executors.newcachedthreadpool (); Exec.execute (new Waxoff (car)); Exec.execute (new Waxon (car)); TimeUnit.SECONDS.sleep (5); Exec.shutdownnow ();//Interrupt All Tasks//Shutdownnow attempt to stop all active tasks being performed, suspend processing of the awaited task, and return to the list of tasks awaiting execution}} Class Car {private lock lock = new Reentrantlock ();p rivate Condition Condition = lock.newcondition ();//management of the task of communication, does not contain any concerns State information//means polishing, waxing processing status Private Boolean Waxon = false;//waxing public/* synchronized */void waxed () {lock.lock (); try {Waxon = True ;//Ready to buff//notifyall (); Condition.signaLAll ();} finally {Lock.unlock ();}} Polished public/* synchronized */void buffed () {lock.lock (); try {Waxon = false;//ready to another coat of wax//notifyall (); c Ondition.signalall ();} finally {Lock.unlock ();}} Wait waxing public/* synchronized */void waitforwaxing () throws Interruptedexception {lock.lock (); try {while (Waxon = = False {//wait ();//suspend this task condition.await ();}} finally {Lock.unlock ();}} Wait polished public/*synchronized*/void waitforbuffing () throws Interruptedexception {Lock.lock (), try {while (Waxon = True {//wait ();//suspend this task condition.await ();}} finally {Lock.unlock ();}}} Class Waxon implements Runnable {private Car car;public Waxon (car c) {car = c;} @Overridepublic void Run () {try {while (! Thread.interrupted ()) {System.out.println ("Wax on!"); TimeUnit.MILLISECONDS.sleep (car.waxed);//waxing car.waitforbuffing ();//Polishing}} catch (Interruptedexception e) { System.out.println ("Exit by Interruption");//E.printstacktrace ();} System.out.println ("Ending Wax on Task");}} Class WaxOFF implements Runnable {private Car car;public Waxoff (car c) {car = c;} @Overridepublic void Run () {try {while (! Thread.interrupted ()) {car.waitforwaxing ();//etc Wax System.out.println ("Wax off!"); TimeUnit.MILLISECONDS.sleep (+); car.buffed ();//Polishing}} catch (Interruptedexception e) {System.out.println ("Exit by Break") ;//E.printstacktrace ();} System.out.println ("Ending Wax Off task");}} /* Output: (95% match) wax off!wax on!... wax on!wax off!wax on!wax off!wax on!wax off!wax on!wax off!wax on!wax off!wax o N!wax off!wax On!wax off! through interrupt exit ending wax off taskending wax on task */


Java threads use the displayed lock and condition

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.