我是编程新手,所以我决定写一个简单的多线程程序。它显示了餐厅的工作。客户点菜,服务员服务和厨师准备。但是我遇到了一个问题,我认为这是死锁的情况,因为当我运行它时,它会打印“排序”而没有其他内容。我不明白什么是错的。请帮忙。谢谢。Java中的多线程
Restaurant.java
public class Restaurant implements Runnable{
Client cl=new Client();
Chef ch=new Chef();
Waiter w=new Waiter();
public synchronized void makeOrder() throws InterruptedException{
notifyAll();
cl.makeOrder();
wait();
}
public synchronized void makeServing() throws InterruptedException{
notifyAll();
wait();
}
public synchronized void makeFood() throws InterruptedException{
notifyAll();
ch.makeFood();
Thread.sleep(1000);
wait();
}
@Override
public void run() {
try {
for(int i=0;i<10;i++){
makeOrder();
makeServing();
makeFood();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Client.java
public class Client{
public void makeOrder(){
System.out.println("Ordering"+Thread.currentThread().getId());
}
Waiter.java
public class Waiter {
public void makeServe() {
System.out.println("Serving order"+Thread.currentThread().getId());
}
Chef.java
public class Chef {
public void makeFood(){
System.out.println("Making food "+Thread.currentThread().getId());
}
Main.java
public class Main {
public static void main(String[] args) {
Restaurant r=new Restaurant();
Thread t=new Thread(r);
t.start();
}
}
_But我有一个问题,我认为这是deadlocking_的情况;您将不得不提供您正面临的_problem_的详细信息 – Nivas 2011-06-17 08:40:07
另外,请提供线程正在实例化的代码。 – Jivings 2011-06-17 08:42:10
你的主要方法是什么样的?您发布的代码看起来很奇怪,但我无法看到那里出现死锁的原因。 – Kaj 2011-06-17 08:43:04