Java thread Deadlock (explanation of the Philosopher's dining case)-------thinking Java 4

Source: Internet
Author: User
Tags lenovo

Package org.rui.thread.deadlock;/** * Lock * *  chopsticks * *  @author Lenovo * */public class Chopstick {private Boolean tak En = false;public  synchronized void Take () throws Interruptedexception {while (taken) {wait ();} Taken = true;} Public synchronized void Drop () {taken = False;notifyall ();}}


Package Org.rui.thread.deadlock;import java.util.random;import java.util.concurrent.timeunit;/** * Philosophers * @author Lenovo * */public class philosopher implements Runnable {private chopstick left;private chopstick right;private Final int Id;private Final int ponderfactor;//thinking private Random rand = new Random ();p rivate void Pause () throws Interruptedexcepti on {if (Ponderfactor = = 0) return;{ TimeUnit.MILLISECONDS.sleep (Rand.nextint (Ponderfactor * 250));}}  Public philosopher (chopstick left, chopstick right, int id, int ponderfactor) {this.left = Left;this.right = Right;this.id = Id;this.ponderfactor = Ponderfactor;} @Overridepublic void Run () {try {while (! Thread.interrupted ()) {System.out.println (this + "+" thinking thinking ");p ause ();//pause//philosopher becomes Hungrysystem.ou T.println (This + "" + "grabbing catch Right"); Right.take (); System.out.println (This + "" + "grabbing catch Left"); Left.take (); System.out.println (this + "+" eating Eat ");p ause ();//Pause Right.drop ();//Stop Left.drop ();}} catch (InterRuptedexception e) {System.out.println (this+ "" + "exits by Interrupt");}} @Overridepublic String toString () {return ' philosopher ' + ID;}}


package org.rui.thread.deadlock;import Java.io.ioexception;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import Java.util.concurrent.TimeUnit /** * This program will be a life lock * @author Lenovo * */public class Deadlockingdiningphilosophers {public static void main (string[] args) t Hrows interruptedexception, IOException {String arg[] = {"0", "5", "timeout"};int ponder = 5;if (Arg.length > 0) {Ponde R = Integer.parseint (Arg[0]);} int size = 5;if (Arg.length > 1) {size = Integer.parseint (arg[1]);} Executorservice exec = Executors.newcachedthreadpool (); Chopstick[] Sticks = new Chopstick[size];for (int i = 0; i < size; i++) {sticks[i] = new Chopstick ();} for (int i = 0; i < size; i++) {Exec.execute (new philosopher (Sticks[i], sticks[(i + 1)% size], i,ponder)); if (arg.lengt H==3&&arg[2].equals ("timeout")) {TimeUnit.SECONDS.sleep (5);}    Else{system.out.println ("Press ENTER to quit"); System.in.read ();} Exec.shutdownnow ();}}} 

Package Org.rui.thread.deadlock;import Java.io.ioexception;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import java.util.concurrent.timeunit;/** * Destroy the fourth article to prevent deadlock * @author Lenovo * */public class  fixeddiningphilosophers {public static void main (string[] args) throws interruptedexception,ioexception {string[] arg = { "5", "5", "timeout"};int ponder = 5;if (Arg.length > 0) {ponder = Integer.parseint (Arg[0]);} int size = 5;if (Arg.length > 1) {size = Integer.parseint (arg[1]);} Executorservice exec = Executors.newcachedthreadpool (); Chopstick[] Sticks = new Chopstick[size];for (int i = 0; i < size; i++) {sticks[i] = new Chopstick ();} for (int i = 0; i < size; i++) {//By making sure that the last philosopher first picks up and puts down the left chopstick,//we can remove the deadlock so that the program runs smoothly. if (I < (size-1)) {Exec.execute (new philosopher (sticks[i], sticks[i + 1], i,ponder));} else {Exec.execute (new Philoso Pher (Sticks[0], sticks[i], I, ponder));}} if (arg.length = = 3 && arg[2].equals ("timeout")){TimeUnit.SECONDS.sleep (5);} else {System.out.println ("press ENTER to quit"); System.in.read ();} Exec.shutdownnow ();}}



Java thread Deadlock (explanation of the Philosopher's dining case)-------thinking Java 4

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.