主题:哲学进餐问题
import java.util.concurrent.*;
class Chopstick //筷子
{
private boolean taken=false;
static int count=0;
final int id=count++;
public Chopstick()
{
}
public synchronized void take()
{
try
{
while(taken==true)
wait();
taken=true;
}
catch(InterruptedException e)
{
System.out.println("ssssssssssssssssssssssssssssss"+id);
}
}
public synchronized void drop()
{
taken=false;
notifyAll();
}
}
class Philosopher implements Runnable //哲学家
{
private Chopstick left;
private Chopstick right;
private final int id;
public Philosopher(Chopstick left,Chopstick right,int id)
{
this.left=left;
this.right=right;
this.id=id;
}
public void run()
{
try
{
while(!Thread.interrupted())
{
System.out.println(this+" is thinking!");
TimeUnit.MILLISECONDS.sleep(100);
System.out.println(this+" left!");
left.take();
System.out.println(this+" right!");
right.take();
System.out.println(this+" is eating!");
TimeUnit.MILLISECONDS.sleep(200);
left.drop();
right.drop();
}
}
catch(InterruptedException e)
{
System.out.println(this+" Interrupted");
}
}
public String toString()
{
return "Philosopher "+id;
}
}
public class DiningPhilosophers //哲学家进餐
{
public static void main(String[] args)throws Exception
{
int size=3;//当size为单数是为什么程序无法中断,而复数则可以。
ExecutorService exec=Executors.newCachedThreadPool();
Chopstick []chopsticks=new Chopstick[size];
for(int i=0;i<size;i++)
{
chopsticks[i]=new Chopstick();
}
for(int i=0;i<size;i++)
{
exec.execute(new Philosopher(chopsticks[i],chopsticks[(i+1)%size],i));
}
TimeUnit.SECONDS.sleep(1);
exec.shutdownNow();
}
}
问题:当size为单数是为什么程序无法中断,而复数则可以。
class Chopstick //筷子
{
private boolean taken=false;
static int count=0;
final int id=count++;
public Chopstick()
{
}
public synchronized void take()
{
try
{
while(taken==true)
wait();
taken=true;
}
catch(InterruptedException e)
{
System.out.println("ssssssssssssssssssssssssssssss"+id);
}
}
public synchronized void drop()
{
taken=false;
notifyAll();
}
}
class Philosopher implements Runnable //哲学家
{
private Chopstick left;
private Chopstick right;
private final int id;
public Philosopher(Chopstick left,Chopstick right,int id)
{
this.left=left;
this.right=right;
this.id=id;
}
public void run()
{
try
{
while(!Thread.interrupted())
{
System.out.println(this+" is thinking!");
TimeUnit.MILLISECONDS.sleep(100);
System.out.println(this+" left!");
left.take();
System.out.println(this+" right!");
right.take();
System.out.println(this+" is eating!");
TimeUnit.MILLISECONDS.sleep(200);
left.drop();
right.drop();
}
}
catch(InterruptedException e)
{
System.out.println(this+" Interrupted");
}
}
public String toString()
{
return "Philosopher "+id;
}
}
public class DiningPhilosophers //哲学家进餐
{
public static void main(String[] args)throws Exception
{
int size=3;//当size为单数是为什么程序无法中断,而复数则可以。
ExecutorService exec=Executors.newCachedThreadPool();
Chopstick []chopsticks=new Chopstick[size];
for(int i=0;i<size;i++)
{
chopsticks[i]=new Chopstick();
}
for(int i=0;i<size;i++)
{
exec.execute(new Philosopher(chopsticks[i],chopsticks[(i+1)%size],i));
}
TimeUnit.SECONDS.sleep(1);
exec.shutdownNow();
}
}
问题:当size为单数是为什么程序无法中断,而复数则可以。