簡體   English   中英

Java 多線程 - 我該如何解決這個死鎖?

[英]Java Multithreading - How can I solve this deadlock?

所以我正在使用多線程進行火車模擬,但遇到了問題。 我想要發生的是,一列火車會停在一個車站讓乘客上車,然后在一定數量的乘客上車后,火車就會開往下一個車站,做同樣的事情,讓乘客上車板,然后 go 到下一站,依此類推。 我得到的 output 如下所示。 火車成功到達下一站,但它只是卡在那里,從來沒有讓任何乘客上車。 我很清楚這是由於我的代碼中的 wait() 而發生的。 我想就在這種情況下我能做些什么征求您的建議,並希望得到一些指導,為我指明正確的方向

The train has arrived at station 1
Passenger has arrived at the station
Passenger is boarding the train
Passenger has arrived at the station
Passenger is boarding the train
Passenger has arrived at the station
Passenger is boarding the train
Passenger has arrived at the station
Passenger is boarding the train
Passenger has arrived at the station
Passenger is boarding the train
The train will now be leaving the station
The train will now be arriving at the next station
The train has arrived at station 2
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;

public class Main {
    public static void main(String[] args) {
    Station s = new Station();  
    Train t = new Train(s);
    new Thread(t).start();
    passenger p = new passenger(t,s);
    new Thread(p).start();
    
    }
    
    
}
class Train implements Runnable {
    Station s;
    public Train (Station s) {
        this.s = s;
    }
    public void run() {
        s.arriveAtStation();
        
    }
}

class passenger implements Runnable {
    Train t;
    Station s;
    public passenger(Train t, Station s){
        this.t = t;
        this.s = s;
    }
    public void run() {
        s.waitForPassengers(); 
    }
 }

import java.util.concurrent.CountDownLatch;

public class Station {

    int stations = 5;
    int currentStation = 1;
    int npassengers = 0;
    int passengers = 5;
    public synchronized void arriveAtStation() {
        while (currentStation != stations) {
            System.out.println("The train has arrived at station " + currentStation);
            currentStation++;
            try {
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (currentStation == stations) {
            System.out.println("The train has reached the final station");
        }
    }
    public synchronized void waitForPassengers() {
        while(npassengers != passengers) {
            System.out.println("Passenger has arrived at the station");
            try {
                Thread.sleep(1000);
            }
            catch (Exception e) {}
            System.out.println("Passenger is boarding the train");
            npassengers++;
        }
        
        
        System.out.println("The train will now be leaving the station");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("The train will now be arriving at the next station");
        notify();
        
    }
}

到達下一站后,您不知何故需要啟動另一個 Thread Passenger。

解釋:

您的 Train 調用arriveAtStation()並通過調用wait()阻止。 您的乘客調用waitForPassengers() ,每秒增加 1 個乘客,直到 5 個。然后它調用notify() ,喚醒您的火車線程。

然后它就完成了。

您的 Train-Thread 進入while (currentStation != stations)的第二次迭代,再次調用wait() 但是現在沒有更多的乘客線程可以再次調用notify() ,因為它已經在第一次迭代中完成了。


編輯:解決方案的一些提示:

  1. 每 X 秒運行一次 Java 線程
  2. Main中使用某種循環執行乘客線程
  3. 讓火車線程啟動另一個乘客線程

它們都可能有一些優點和缺點,可能會導致您改進設計。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM