0

これは、本で見つけた駐車場の小さな実装です。Javaスレッドを終了する方法を知りたいです。一定時間経過しても空いている駐車場がない場合は停止したいのですが、私の場合はsleep(500)です。

import java.util.logging.Level;
import java.util.logging.Logger;

class ParkingLot {

// I assume that parking lot capacity is 4 lots
public static final int MAX_CAPACITY = 4;
private int totalParkedCars = 0;
public synchronized void park(String vehicle) {

    // check if there is a free lot
    while (totalParkedCars >= MAX_CAPACITY) {
        try {
            System.out.println("The parking lot is full " +
                    vehicle + " has to wait.");

            // a time that vehicle will wait for a parking lot to free
            wait(500);
 // ! I wait and if no free lots want to stop thread !
        } catch (InterruptedException e) {
                       Logger.getLogger(ParkingLot.class.getName()).log(Level.SEVERE, 
                    "Sorry for inconvenience :(", e);
        }

    }
    // precondition is true
    System.out.println(vehicle + " has parked");
    totalParkedCars++;
}
public synchronized void leave(String vehicle) {
    totalParkedCars--;
    System.out.println(vehicle + " has left, notify a waiting vehicle.");
    notify();
}
} 

車両クラス:

import java.util.logging.Level;
import java.util.logging.Logger;

class Vehicle extends Thread {
private ParkingLot parking;
private String name;
private volatile boolean running = true;
public Vehicle(String name, ParkingLot parking) {
    this.name = name;
    this.parking = parking;
    start();
}
public void terminate() {
    running = false;
}
public void run() {
    while (running) {
        System.out.println(this.name + " is ready to park.");
        parking.park(this.name);
        try {
            // Vehicle waits at lot
            sleep(1000);  // TODO: random time to wait                  
        } catch (InterruptedException e) {
            // Will catch and log InterruptedException if happens
            Logger.getLogger(Vehicle.class.getName()).log(Level.SEVERE, this.name, e);
            running = false;
        }

        // Vehicle leaves parking lot
        parking.leave(this.name);   
        running = false;
    }
}
 }

テスト用のクラス:

public class GSTest {
public static void main(String[] args) {
    ParkingLot parkingLot = new ParkingLot();
    new Vehicle("Vehicle_1", parkingLot);
    new Vehicle("Vehicle_2", parkingLot);
    new Vehicle("Vehicle_3", parkingLot);
    new Vehicle("Vehicle_4", parkingLot);
    new Vehicle("Vehicle_5", parkingLot);
    new Vehicle("Vehicle_6", parkingLot);
}
}
4

2 に答える 2

2

アドバイスはいくつかのレベルで提供できます。

  1. Java 割り込みメカニズムを使用しyourThread.interrupt()ます。スレッド内で呼び出して、InterruptedException入ったときsleep(または既に眠っているとき) を取得します。

  2. スレッドを外側から止めないでください。スレッド内で条件を使用します。

  3. sleepこれが最良の選択肢です。;で無限ループを使用しないでください。代わりに、必要なときにキャンセルできる繰り返しタスクをスケジュールします。

于 2012-12-01T18:01:07.453 に答える
1
boolean waited = false;

while (totalParkedCars >= MAX_CAPACITY) {
    try {
        System.out.println("The parking lot is full " +
                vehicle + " has to wait.");

        if (!waited) {
            waited = true;
            wait(500);
        } else {
            return;
        }
    } catch (InterruptedException e) {
        // ...
    }
}

ものすごく単純。

于 2012-12-01T17:57:36.670 に答える