Parking Problem
There's n parking lots. On one parking lot, there can be only one car at a time. If all parking lots are occupied then the car will wait some time and if there's still no free parking lot then it leaves.
需要使用线程来解决(通过意志同步)。
这是我的代码:
parking
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个 parking 场和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 个 parking 场和 4 辆车时):所有第一辆车(1 和 2)都停在 parking 场,其他车(3 和 4)就离开了,因为没有免费 parking 场。即使有大约 15 辆车,他们仍然无法进入那里。
那么我怎样才能让汽车在出发前等待一段时间呢?如果有免费 parking 场,他们就会去 parking ,否则他们就会离开 parking 场。
最佳答案
修改了您的代码,看看是否有效。
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)类主要{
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();
}
}
}
输出:
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
关于java - Java多线程如何让线程等待一段时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43282088/