/*** book: "Thinking In Java" * Features: Philosophers eating problems * as philosophers, they are poor, so they can only buy five chopsticks. They sat around the table and put a chopstick between each person. When a philosopher wants to eat, the philosopher must also get the chopsticks on the left and right. If the chopsticks are already being used by a philosopher on the left or right, the philosopher must wait until he can get the necessary chopsticks * File: chopstick.java* time: May 9, 2015 15:08:21* Author: cutter_point*/ Package Lesson21concurency;public class Chopstick{private Boolean taken = false;//chopsticks have been picked up public synchronized void Take () throws Interruptedexception{while (taken) this.wait (); If the chopsticks are picked up, then you have to wait and wait for the other process to wake him up taken = true; If not picked up, then call this function chopsticks are taken up}public synchronized void drop ()//This function is put down chopsticks {taken = False;this.notifyall ();//Wake up others, This chopstick can be used again}}
/*** book: "Thinking In Java" * Features: Philosophers eating problems * as philosophers, they are poor, so they can only buy five chopsticks. They sat around the table and put a chopstick between each person. When a philosopher wants to eat, the philosopher must also get the chopsticks on the left and right. If the chopsticks are already being used by a philosopher on the left or right, the philosopher must wait until he can get the necessary chopsticks * File: philosopher.java* time: May 9, 2015 15:17:36* Author: cutter_point*/ Package Lesson21concurency;import Java.util.random;import Java.util.concurrent.timeunit;import static net.mindview.util.print.*;p Ublic class philosopher implements runnable//a philosopher thread {//philosopher on both sides there are two pairs of chopsticks, the philosopher got the ID number, gave a data, Used to judge whether the philosopher is thinking or dining private chopstick left;//left chopsticks private chopstick right;//right chopsticks private final int id;//This philosopher get ID number private final int ponderfactor; A data to determine whether to think or dine private random rand = new Random (998);p ublic philosopher (chopstick left, chopstick right, int ident, in T ponder) {this.left = Left;this.right = Right;this.id = ident;//Philosopher's id number this.ponderfactor = ponder;//Philosophers Thinking data}private Voi D pause () throws interruptedexception{if (This.ponderfactor = = 0) {//If the philosopher is thinking that the data is 0, then it is not necessary to pause thinking and return directly;} If the philosopher wants to think, then we'll let him pause for a while. TimeUnit.MILLISECONDS.sleep (Rand.nextint.Ponderfactor * 1500)); Think Time}public String toString () {return "philosopher:" + this.id + "number";} @Overridepublic void Run () {//Philosopher's Mode of action//Receive first, if the philosopher is still on the table, that is not interrupted try{while (! Thread.interrupted ()) {///1, first the philosopher will think of print (this + "+" in thinking "); This.pause (); Think for a time//2, think for a while to feel hungry, then the philosopher will eat, then the left and right to pick up the chopsticks, and then began to eat, meal time will be a this.right.take (); Pick up the chopsticks print (this + "+" to pick up the chopsticks on the right); This.left.take ();p rint (this + "+" pick up the chopsticks on the left);p rint (this + "+" start eating "); This.pause () ; Eat time//3, after eating, will put down the chopsticks print (this + "+" ate, put down chopsticks "); This.left.drop (); This.right.drop ();} catch (Interruptedexception e) {e.printstacktrace ();p rint (this + "+" was interrupted to exit the table.) ");}}}
/*** book: "Thinking In Java" * Features: Philosophers eating problems * as philosophers, they are poor, so they can only buy five chopsticks. They sat around the table and put a chopstick between each person. When a philosopher wants to eat, the philosopher must also get the chopsticks on the left and right. If the chopsticks are already being used by a philosopher on the left or right, then the philosopher must wait until he can get the necessary chopsticks * File: deadlockingdiningphilosophers.java* time: May 9, 2015 15:36:13* Author: cutter_point*/package Lesson21concurency;import java.util.concurrent.executorservice;import Java.util.concurrent.executors;import Java.util.concurrent.timeunit;public class deadlockingdiningphilosophers{ public static void Main (string[] args) throws exception{int ponder = 5;int size = 5;//philosopher Executorservice exec = executors . Newcachedthreadpool (); Thread connection Pool chopstick[] Sticks = new chopstick[size];//Create an array space for (int i = 0; i < size; ++i) sticks[i] = new chopstick ();//initialization number The objects inside the group for (int i = 0; i < size; ++i) {Exec.execute (new philosopher (Sticks[i], sticks[(i+1)% size], I, ponder));} if (args.length = = 3 && args[2].equals ("timeout")) TimeUnit.SECONDS.sleep (5); Else{system.out.println ("Enter Exit" ); System.in.read ();} Exec.shutdownnow ();//Stop All Threads}}
Output:
Enter exit
Philosopher: number 4th is thinking obj1
Philosopher: number 1th is thinking obj1
Philosopher: number 3rd is thinking obj1
Philosopher: Number 2nd is thinking obj1
Philosopher: number No. 0 is thinking obj1
Philosopher: number 3rd pick up the chopsticks on the right obj1
Philosopher: Number 2nd pick up the chopsticks on the right obj1
Philosopher: number 1th pick up the chopsticks on the right obj1
Philosopher: number No. 0 pick up the chopsticks on the right obj1
Philosopher: number 4th pick up the chopsticks on the right obj1
Java.lang.InterruptedException
Philosopher: number 3rd was interrupted and withdrew from the table. Obj1
Philosopher: number No. 0 was interrupted and withdrew from the table. Obj1
Philosopher: number 1th was interrupted and withdrew from the table. Obj1
At java.lang.Object.wait (Native Method)
At Java.lang.Object.wait (object.java:503)
At Lesson21Concurency.Chopstick.take (chopstick.java:18)
At Lesson21Concurency.Philosopher.run (philosopher.java:65)
At Java.util.concurrent.ThreadPoolExecutor.runWorker (threadpoolexecutor.java:1145)
At Java.util.concurrent.threadpoolexecutor$worker.run (threadpoolexecutor.java:615)
At Java.lang.Thread.run (thread.java:744)
Java.lang.InterruptedException
At java.lang.Object.wait (Native Method)
At Java.lang.Object.wait (object.java:503)
At Lesson21Concurency.Chopstick.take (chopstick.java:18)
At Lesson21Concurency.Philosopher.run (philosopher.java:65)
At Java.util.concurrent.ThreadPoolExecutor.runWorker (threadpoolexecutor.java:1145)
At Java.util.concurrent.threadpoolexecutor$worker.run (threadpoolexecutor.java:615)
At Java.lang.Thread.run (thread.java:744)
Java.lang.InterruptedException
At java.lang.Object.wait (Native Method)
At Java.lang.Object.wait (object.java:503)
At Lesson21Concurency.Chopstick.take (chopstick.java:18)
At Lesson21Concurency.Philosopher.run (philosopher.java:65)
At Java.util.concurrent.ThreadPoolExecutor.runWorker (threadpoolexecutor.java:1145)
At Java.util.concurrent.threadpoolexecutor$worker.run (threadpoolexecutor.java:615)
At Java.lang.Thread.run (thread.java:744)
Java.lang.InterruptedException
At java.lang.Object.wait (Native Method)
At Java.lang.Object.wait (object.java:503)
At Lesson21Concurency.Chopstick.take (chopstick.java:18)
At Lesson21Concurency.Philosopher.run (philosopher.java:65)
At Java.util.concurrent.ThreadPoolExecutor.runWorker (threadpoolexecutor.java:1145)
At Java.util.concurrent.threadpoolexecutor$worker.run (threadpoolexecutor.java:615)
At Java.lang.Thread.run (thread.java:744)
Philosopher: Number 2nd was interrupted and withdrew from the table. Obj1
Java.lang.InterruptedException
At java.lang.Object.wait (Native Method)
At Java.lang.Object.wait (object.java:503)
At Lesson21Concurency.Chopstick.take (chopstick.java:18)
At Lesson21Concurency.Philosopher.run (philosopher.java:65)
At Java.util.concurrent.ThreadPoolExecutor.runWorker (threadpoolexecutor.java:1145)
At Java.util.concurrent.threadpoolexecutor$worker.run (threadpoolexecutor.java:615)
At Java.lang.Thread.run (thread.java:744)
Philosopher: number 4th was interrupted and withdrew from the table. Obj1
This can lead to deadlocks, that is, the limited resources that are shared is not enough, when the multiple threads scramble for the situation
Improved:
/*** book: "Thinking In Java" * Features: Philosophers eating problems * as philosophers, they are poor, so they can only buy five chopsticks. They sat around the table and put a chopstick between each person. When a philosopher wants to eat, the philosopher must also get the chopsticks on the left and right. If the chopsticks are already being used by a philosopher on the left or right, then the philosopher must wait until he can get the necessary chopsticks * File: fixeddiningphilosophers.java* time: May 9, 2015 15:36:13* Author: Cutter_point*/package Lesson21concurency;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import Java.util.concurrent.timeunit;public class Fixeddiningphilosophers{public static void Main (string[] args) throws exception{int ponder = 5;int size = 5;//philosopher Executorservice exec = EXECUTORS.NEWCAC Hedthreadpool (); Thread connection Pool chopstick[] Sticks = new chopstick[size];//Create an array space for (int i = 0; i < size; ++i) sticks[i] = new chopstick ();//initialization number The objects inside the group for (int i = 0; i < size; ++i) {//exec.execute (new philosopher (Sticks[i], sticks[(i+1)% size], I, ponder)); if (i &l T (size-1)) Exec.execute (new philosopher (sticks[0], sticks[i+1], I, ponder)); Elseexec.execute (new philosopher (sticks[0], sticks[ I], I, ponder));} if (args.length = = 3 && args[2].equals ("timeout")) TimeUnit.SECONDS.sleep (5); Else{system.out.println ("Enter Exit"); System.in.read ();} Exec.shutdownnow ();//Stop All Threads}}
Output:
Enter exit
Philosopher: Number 2nd is thinking obj1
Philosopher: number No. 0 is thinking obj1
Philosopher: number 4th is thinking obj1
Philosopher: number 1th is thinking obj1
Philosopher: number 3rd is thinking obj1
Philosopher: Number 2nd pick up the chopsticks on the right obj1
Philosopher: No. 2nd pick up the chopsticks on the left Obj1
Philosopher: No. 2nd begins to eat obj1
Philosopher: number 4th pick up the chopsticks on the right obj1
Philosopher: number 1th pick up the chopsticks on the right obj1
Philosopher: number No. 0 pick up the chopsticks on the right obj1
Philosopher: No. 2nd is finished, put down the chopsticks obj1
Philosopher: Number 2nd is thinking obj1
Philosopher: No. No. 0 pick up the chopsticks on the left Obj1
Philosopher: No. No. 0 begins to eat obj1
Philosopher: No. No. 0 is finished, put down the chopsticks obj1
Philosopher: number No. 0 is thinking obj1
Philosopher: No. 4th pick up the chopsticks on the left Obj1
Philosopher: No. 4th begins to eat obj1
Philosopher: No. 4th is finished, put down the chopsticks obj1
Philosopher: number 4th is thinking obj1
Philosopher: No. 1th pick up the chopsticks on the left Obj1
Philosopher: number 3rd pick up the chopsticks on the right obj1
Philosopher: No. 1th begins to eat obj1
Philosopher: No. 1th is finished, put down the chopsticks obj1
Philosopher: number 1th is thinking obj1
Philosopher: No. 3rd pick up the chopsticks on the left Obj1
Philosopher: No. 3rd begins to eat obj1
Philosopher: No. 3rd is finished, put down the chopsticks obj1
Philosopher: number 3rd is thinking obj1
Philosopher: Number 2nd pick up the chopsticks on the right obj1
Philosopher: No. 2nd pick up the chopsticks on the left Obj1
Philosopher: No. 2nd begins to eat obj1
Philosopher: number No. 0 pick up the chopsticks on the right obj1
Philosopher: number 4th pick up the chopsticks on the right obj1
Philosopher: number 1th pick up the chopsticks on the right obj1
Philosopher: No. 2nd is finished, put down the chopsticks obj1
Philosopher: Number 2nd is thinking obj1
Philosopher: No. 1th pick up the chopsticks on the left Obj1
Philosopher: No. 1th begins to eat obj1
Philosopher: Number 2nd pick up the chopsticks on the right obj1
Philosopher: No. 1th is finished, put down the chopsticks obj1
Philosopher: number 1th is thinking obj1
Philosopher: No. 2nd pick up the chopsticks on the left Obj1
Philosopher: No. 2nd begins to eat obj1
Philosopher: number 1th pick up the chopsticks on the right obj1
Philosopher: No. 2nd is finished, put down the chopsticks obj1
Philosopher: Number 2nd is thinking obj1
Philosopher: No. 1th pick up the chopsticks on the left Obj1
Philosopher: No. 1th begins to eat obj1
Philosopher: Number 2nd pick up the chopsticks on the right obj1
Philosopher: No. 1th is finished, put down the chopsticks obj1
Philosopher: number 1th is thinking obj1
Philosopher: No. 2nd pick up the chopsticks on the left Obj1
Philosopher: No. 2nd begins to eat obj1
Philosopher: number 1th pick up the chopsticks on the right obj1
Java.lang.InterruptedException
Philosopher: number 3rd was interrupted and withdrew from the table. Obj1
Philosopher: number No. 0 was interrupted and withdrew from the table. Obj1
At java.lang.Object.wait (Native Method)
At Java.lang.Object.wait (object.java:503)
At Lesson21Concurency.Chopstick.take (chopstick.java:18)
At Lesson21Concurency.Philosopher.run (philosopher.java:63)
At Java.util.concurrent.ThreadPoolExecutor.runWorker (threadpoolexecutor.java:1145)
At Java.util.concurrent.threadpoolexecutor$worker.run (threadpoolexecutor.java:615)
At Java.lang.Thread.run (thread.java:744)
Java.lang.InterruptedException
At java.lang.Object.wait (Native Method)
At Java.lang.Object.wait (object.java:503)
At Lesson21Concurency.Chopstick.take (chopstick.java:18)
At Lesson21Concurency.Philosopher.run (philosopher.java:65)
At Java.util.concurrent.ThreadPoolExecutor.runWorker (threadpoolexecutor.java:1145)
At Java.util.concurrent.threadpoolexecutor$worker.run (threadpoolexecutor.java:615)
At Java.lang.Thread.run (thread.java:744)
Java.lang.InterruptedException:sleep interrupted
At Java.lang.Thread.sleep (Native Method)
At Java.lang.Thread.sleep (thread.java:340)
At Java.util.concurrent.TimeUnit.sleep (timeunit.java:360)
At Lesson21Concurency.Philosopher.pause (philosopher.java:42)
At Lesson21Concurency.Philosopher.run (philosopher.java:68)
At Java.util.concurrent.ThreadPoolExecutor.runWorker (threadpoolexecutor.java:1145)
At Java.util.concurrent.threadpoolexecutor$worker.run (threadpoolexecutor.java:615)
At Java.lang.Thread.run (thread.java:744)
Philosopher: Number 2nd was interrupted and withdrew from the table. Obj1
Java.lang.InterruptedException
At java.lang.Object.wait (Native Method)
At Java.lang.Object.wait (object.java:503)
At Lesson21Concurency.Chopstick.take (chopstick.java:18)
At Lesson21Concurency.Philosopher.run (philosopher.java:65)
At Java.util.concurrent.ThreadPoolExecutor.runWorker (threadpoolexecutor.java:1145)
At Java.util.concurrent.threadpoolexecutor$worker.run (threadpoolexecutor.java:615)
At Java.lang.Thread.run (thread.java:744)
Java.lang.InterruptedException Philosopher: number 4th was interrupted and withdrew from the table. Obj1
At java.lang.Object.wait (Native Method)
At Java.lang.Object.wait (object.java:503)
At Lesson21Concurency.Chopstick.take (chopstick.java:18)
At Lesson21Concurency.Philosopher.run (philosopher.java:65)
At Java.util.concurrent.ThreadPoolExecutor.runWorker (threadpoolexecutor.java:1145)
At Java.util.concurrent.threadpoolexecutor$worker.run (threadpoolexecutor.java:615)
At Java.lang.Thread.run (thread.java:744)
Philosopher: number 1th was interrupted and withdrew from the table. Obj1
This is not a deadlock, as long as you return to the end of the match between the various threads
"Thinkinginjava" 63, philosophers eating problems