The Traffic Signal Method of the production consumer mode in one of the deadlock Solutions

Source: Internet
Author: User
Package tread;/*** producer consumer mode: Traffic Signal Method * Wait (): Wait, release lock; * notify (); * Wait and consumer y must be used together with sychronized; * Sleep (): Sleep With lock: **/public class movie {private string PIC; private Boolean flag = true; // signal lamp // flag = T: production, consumer waiting, notification consumption after production is complete: // flag = F: consumer consumption, producer waiting, notification production after consumption is complete; Public movie (string pic) {super (); this. PIC = PIC;} public movie () {}/ *** playback function ** @ throws interruptedexception */Public synchronized void play (strin G pic) {If (! Flag) {try {This. wait ();} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace () ;}try {thread. sleep (500);} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace ();} This. PIC = PIC; system. out. println ("produced" + pic); this. Y (); this. flag = false;}/*** viewing function */Public synchronized void Watch () {If (FLAG) {try {This. wait ();} catch (interruptedexception e) {e. printstacktrace () ;}try {thread. sleep (200);} catch (interruptedexception e) {e. printstacktrace ();} This. Y (); this. flag = true; system. out. println ("consumed" + pic) ;}} package tread; public class player implements runnable {private movie m; @ overridepublic void run () {for (INT I = 0; I <20; I ++) {If (0 = I % 2) {M. play ("Zuo Qinglong");} else {M. play ("") ;}} public player (movie) {super (); this. M = movie ;}} package tread; public class watcher implements runnable {private movie m; @ overridepublic void run () {for (INT I = 0; I <20; I ++) {M. watch () ;}} public watcher (movie) {super (); this. M = movie ;}} package tread; public class app {public static void main (string [] ARGs) {movie M = new movie (); player P = new player (m); watcher W = New watcher (m); New thread (P ). start (); New thread (W ). start ();}}

Movie is a public resource;

The Traffic Signal Method of the production consumer mode in one of the deadlock Solutions

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.