2017-04-07 7 views
0

を待つために、スレッドを作るためにどのようにマルチスレッドのJavaあなたは私がこの問題を解決することができます:。いくつかの時間

駐車場の問題
「はありますn個の駐車場1つの駐車場には、一つだけの車があることができすべての駐車場が占有されていれば、車はしばらく待っていて、無料の駐車場がまだ残っていなければ、それは去っていく」

スレッドを使用して解決する必要があります(意志に同期)。

はここ何私のコード:

class Parking implements Runnable { 
private Thread thread; 
private String threadName; 
static int parkingLots; 

static { 
    parkingLots = 5; 
} 

Parking(String threadName) { 
    this.threadName = threadName; 
} 

public void run() { 
    if (parkingLots > 0) { 
     long restTime = (long) (Math.random() * 2000); 
     try { 
      parkingLots--; 
      System.out.println("Car " + threadName + " stands in the parking lot"); 
      Thread.sleep(restTime); 
     } catch (InterruptedException e) { 
     } 
     parkingLots++; 
     System.out.println("Car " + threadName + " has left parking, it stood there" + ((double)restTime/(double)1000) + " s"); 
    } else 
     System.out.println("Car " + threadName + " has left parking"); 
} 

public void start() { 
    if (thread == null) { 
     thread = new Thread(this, threadName); 
     thread.start(); 
    } 
} 
} 

メイン
駐車場私が見たいと思って何

public class Main { 
    public static void main(String[] args) { 
     ArrayList<Parking> parking = new ArrayList<Parking>(); 

     for (int i = 0; i < 15; i++) { 
      parking.add(new Parking(String.valueOf(i + 1))); 
     } 
     for (Parking i: parking) { 
      i.start(); 
     } 
    } 
} 

(2つの駐車場や4台がある場合):

Car 1 stands in the parking lot 
Car 2 stands in the parking lot 
Car 3 is waiting 
Car 4 is waiting 
Car 3 has left parking 
Car 2 has left parking, it stood there 1.08 s 
Car 4 stands in the parking lot 
Car 1 has left parking, it stood there 1.71 s 
Car 4 has left parking, it stood there 0.83 s 

しかし、私は(駐車場2台と車4台の場合)、駐車場に立っているすべての最初の車(1と2)と他の車(3と4)は無料の駐車場がないので、たとえ15台の車があっても、彼らはまだそこに入ることができません。

車を離れる前にしばらく待つことができますか?無料の駐車場がある場合、駐車場を残すために駐車場があります。

助けていただけたら幸いです!ありがとう!

+1

、このコードがあることをいくつかの深刻なリファクタリングを必要とします –

+1

'Runnable'の中に' Thread'を置かないでください。それは別の方法です。プライマリクラスをコーディネーターと考えてください。 'Runnable#run'メソッドを持つ' ParkingLot'型と 'Car'型のスレッドとインスタンス化の詳細を管理します。 –

答えて

1

コードを修正しました。これが動作するかどうかを確認してください。

public class Parking implements Runnable { 
    private Thread thread; 
    private String threadName; 
    static int parkingLots; 

    static { 
     parkingLots = 5; 
    } 

    Parking(String threadName) { 
     this.threadName = threadName; 
    } 

    public void run() { 
     long restTime = (long) (Math.random() * 2000); 
     if (parkingLots > 0) { 
      checkparking(); 
     } else { 
      try { 
       System.out.println("Car " + threadName + " is waiting"); 
       Thread.sleep(restTime); 
       System.out.println("Car " + threadName + " is checking for free parkinglot"); 
       checkparking(); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

    } 

    public void checkparking() { 
     if (parkingLots > 0) { 
     long restTime = (long) (Math.random() * 2000); 
     try { 
      parkingLots--; 
      System.out.println("Car " + threadName + " stands in the parking lot"); 
      Thread.sleep(restTime); 
     } catch (InterruptedException e) { 
     } 
     parkingLots++; 
     System.out.println(
       "Car " + threadName + " has left parking, it stood there" + ((double) restTime/(double) 1000) + " s"); 

    } else { 
     System.out.println(
       "Car " + threadName + " has left since there is no parking space"); 
    } 
    } 

    public void start() { 
     if (thread == null) { 
      thread = new Thread(this, threadName); 
      thread.start(); 
     } 
    } 

} 

パブリッククラスメイン{

 public static void main(String[] args) { 
      ArrayList<Parking> parking = new ArrayList<Parking>(); 

      for (int i = 0; i < 15; i++) { 
       parking.add(new Parking(String.valueOf(i + 1))); 
      } 
      for (Parking i: parking) { 
       i.start(); 
      } 
     } 
} 

出力:あなたが使用していないと、同期は、あなたのクラスはRunnableを実装してはならないし、使用している意味論が正しくありません

Car 2 stands in the parking lot 
Car 1 stands in the parking lot 
Car 7 is waiting 
Car 5 stands in the parking lot 
Car 3 stands in the parking lot 
Car 6 is waiting 
Car 4 stands in the parking lot 
Car 9 is waiting 
Car 8 is waiting 
Car 10 is waiting 
Car 11 is waiting 
Car 12 is waiting 
Car 13 is waiting 
Car 14 is waiting 
Car 15 is waiting 
Car 4 has left parking, it stood there0.049 s 
Car 14 is checking for free parkinglot 
Car 14 stands in the parking lot 
Car 5 has left parking, it stood there0.366 s 
Car 2 has left parking, it stood there0.461 s 
Car 12 is checking for free parkinglot 
Car 12 stands in the parking lot 
Car 15 is checking for free parkinglot 
Car 15 stands in the parking lot 
Car 1 has left parking, it stood there0.882 s 
Car 9 is checking for free parkinglot 
Car 9 stands in the parking lot 
Car 10 is checking for free parkinglot 
Car 10 has left since there is no parking space 
Car 3 has left parking, it stood there1.014 s 
Car 13 is checking for free parkinglot 
Car 13 stands in the parking lot 
Car 15 has left parking, it stood there0.937 s 
Car 6 is checking for free parkinglot 
Car 6 stands in the parking lot 
Car 11 is checking for free parkinglot 
Car 11 has left since there is no parking space 
Car 13 has left parking, it stood there0.344 s 
Car 7 is checking for free parkinglot 
Car 7 stands in the parking lot 
Car 8 is checking for free parkinglot 
Car 8 has left since there is no parking space 
Car 7 has left parking, it stood there0.054 s 
Car 14 has left parking, it stood there1.731 s 
Car 9 has left parking, it stood there1.359 s 
Car 12 has left parking, it stood there1.877 s 
Car 6 has left parking, it stood there1.787 s 
0

たとえば、開始車は駐車/離脱できません。マルチスレッドは、同時に複数のスレッドからアクセスされ、矛盾した状態にならないようにするためのものです。基本的にスレッドを作成して何か。

はあなたの構造はこのようなものでなければなりません:あなたは、あなたがこの正しい方法を実装するためのロックオブジェクト、およびいくつかの条件変数を使用する必要があり、同期を使用していない

import java.util.ArrayList; 
import java.util.List; 
import java.util.concurrent.locks.Condition; 
import java.util.concurrent.locks.Lock; 
import java.util.concurrent.locks.ReentrantLock; 

public class Parking { 

    private final Lock monitor = new ReentrantLock(); 
    private final Condition lotAvailable = monitor.newCondition(); 

    private List<Car> parkedCars = new ArrayList<>(); 

    private final int maxCapacity; 
    private int occupied; 

    public Parking(int maxCapacity) { 
     this.maxCapacity = maxCapacity; 
    } 

    private boolean tryPark(Car car, int maxWaitingMillis) throws InterruptedException{ 
     try { 
      monitor.lock(); 
      if(occupied >= maxCapacity){ 
       long nanos = lotAvailable.awaitNanos(maxWaitingMillis); 
       while (occupied >= maxCapacity) { 
        if (nanos <= 0L) 
         return false; 
        nanos = lotAvailable.awaitNanos(nanos); 
       } 
       ++occupied; 
       parkedCars.add(car); 
       return true; 
      } 
      ++occupied; 
      parkedCars.add(car); 
      return true; 
     }catch (InterruptedException ie){ 
      System.out.println(ie.getMessage()); 
      throw ie; 
     }finally { 
      monitor.unlock(); 
     } 

    } 

    private void leave(Car car){ 
     try { 
      monitor.lock(); 
      if(parkedCars.remove(car)) { 
       --occupied; 
       lotAvailable.signal(); 
      } 
     }catch (Exception e){ 
      System.out.println(e.getMessage()); 
     }finally { 
      monitor.unlock(); 
     } 
    } 


} 
関連する問題