Question Description: A round Table sits 5 philosophers, two of whom have a chopstick, and a table with noodles in the middle. Philosophers think about the problem, when hungry to pick up about two chopsticks to eat, must get two chopsticks to eat. These problems can lead to deadlock, when 5 philosophers pick up their right hand chopsticks, ready to take the left side of the chopsticks when the deadlock phenomenon.
Solution:
1, add a waiter, only after the waiter agreed to take chopsticks, the waiter is responsible for avoiding the deadlock occurred.
2, each philosopher must determine his or her right-hand chopsticks are available, to pick up two chopsticks at the same time to eat, after eating at the same time put down two chopsticks.
3, the provisions of each philosopher must take chopsticks with the number of the small one, so that the last one not to get chopsticks philosopher only the number of large chopsticks, can not pick up, the rest of this chopsticks can be used by other philosophers, to avoid deadlock. This situation does not make good use of resources.
Code implementation: Implementation of the 2nd scenario
Copy Code code as follows:
Package cn.edu.sdust.Philosopher;
/* Each philosopher is equivalent to a thread * *
Class Philosopher extends thread{
private String name;
Private Fork Fork;
Public philosopher (String name,fork Fork) {
Super (name);
This.name=name;
This.fork=fork;
}
public void Run () {
while (true) {
Thinking ();
Fork.takefork ();
Eating ();
Fork.putfork ();
}
}
public void eating () {
System.out.println ("I am Eating:" +name);
try {
Sleep (1000);//simulate eating, occupy a period of time resources
catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
public void Thinking () {
System.out.println ("I am Thinking:" +name);
try {
Sleep (1000);//Simulate thinking
catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
Class fork{
/*5 only chopsticks, the original is not used * *
Private boolean[] Used={false,false,false,false,false,false};
* * Only when the hands of chopsticks are not used, to allow access to chopsticks, and must also obtain both hand and hand chopsticks * *
Public synchronized void Takefork () {
String name = Thread.CurrentThread (). GetName ();
int i = integer.parseint (name);
while (used[i]| | used[(i+1)%5]) {
try {
Wait ()//If the right-hand one is being used and waiting
catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
Used[i]= true;
used[(i+1)%5]=true;
}
/* must also release the chopsticks of the right-hand hand * *
Public synchronized void Putfork () {
String name = Thread.CurrentThread (). GetName ();
int i = integer.parseint (name);
Used[i]= false;
used[(i+1)%5]=false;
Notifyall ()//Wake Up other threads
}
}
Test
public class ThreadTest {
public static void Main (String []args) {
Fork Fork = new Fork ();
New philosopher ("0", fork). Start ();
New Philosopher ("1", fork). Start ();
New Philosopher ("2", fork). Start ();
New Philosopher ("3", fork). Start ();
New Philosopher ("4", fork). Start ();
}
}
Run Result:
Copy Code code as follows:
I am thinking:0
I am Thinking:2
I am Thinking:3
I am Thinking:1
I am Thinking:4
I am eating:0
I am Eating:2
I am thinking:0
I am Eating:4
I am Thinking:2
I am Eating:1
I am Thinking:4
I am Eating:3
I am Thinking:1
I am eating:0
I am Thinking:3
I am Eating:2
I am thinking:0
I am Eating:4
I am Thinking:2
Analysis: The above solution solves the deadlock problem. You can see that there can be only two adjacent eating results, because each moment can meet up to two people at the same time, and the two seats are not adjacent to each other.