我是編程新手,所以我決定寫一個簡單的多線程程序。它顯示了餐廳的工作。客戶點菜,服務員服務和廚師準備。但是我遇到了一個問題,我認爲這是死鎖的情況,因爲當我運行它時,它會打印「排序」而沒有其他內容。我不明白什麼是錯的。請幫忙。謝謝。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