gpt4 book ai didi

Java线程练习: Access control to a limited resource not working

转载 作者:行者123 更新时间:2023-12-02 04:25:50 24 4
gpt4 key购买 nike

我遇到这样的情况:有一家比萨店,有不同数量的顾客。这家比萨店在开业前随机准备数量的比萨饼,并且只有 10 个座位。每个客户都是一个线程。我需要让他们在有空位的情况下同时吃饭。其他人则等待,直到有免费套餐(以及准备好的比萨饼)。我尝试过这个方案,但是这样每个顾客都是单独吃饭。只有当它吃完后,其他顾客才能吃。正确的解决方案是什么?

Pizzeria 的这个方法由每个 Customer 线程调用:

public boolean eatPizza(Integer nPizzas){
if(freeSeats< 1){
try {
wait();
} catch (InterruptedException ex) {
}
}
if(preparedPizzas == 0) //Pizzas terminated for this evening
return false;

freeSeats--;
if(nPizzas > preparedPizzas)
nPizzas = preparedPizzas; //if he wants more pizzas than avaiable ones he have to settle
preparedPizzas-= nPizzas;
try {
Thread.sleep(2000); //Time for eat
} catch (InterruptedException ex) {
}
freeSeats++;
servedCustomers++;
servedPizzas+= nPizzas;
if(preparedPizzas> 0){
notify();
}
else{
notifyAll(); //Pizzas are terminated for this evening
}
return true;
}

客户线程:

public void run(){
int wait = (int)(Math.random() * 5000);
try {
Thread.sleep(wait);
} catch (InterruptedException ex) {
}
nPizzas = (int)(Math.random() * 2 + 1); //He eats 1 or 2 pizzas random
if(pizzeria.eatPizza(nPizzas))
fireEatenEvent();
}

感谢您的帮助

最佳答案

我使用 ExecutorService 创建了一个简单的示例和一个BlockingQueue 。这样您就不必自己处理任何锁。

public class Restaurant {

public static void main(String[] args) throws Exception {
final Random rnd = new Random();
// The queue from which the pizzas are taken
final BlockingQueue<Pizza> pizzas = new LinkedBlockingQueue<>();
// The threadpool that represents the seats of the restaurant
// 10 customers are handled at one time
final ExecutorService seats = Executors.newFixedThreadPool(10);
final int nPizzas = rnd.nextInt(50);
final int nCustomers = rnd.nextInt(100);

System.out.println("There are " + nPizzas + " pizzas for " + nCustomers + " customers.");

// Put some pizzas into the queue
for (int i = 1; i < nPizzas + 1; i++) {
pizzas.put(new Pizza("Pizza " + i));
}

// Marks the end of the queue
pizzas.put(new PoisonPizza());

// Create some customers and send them to the restaurant
for (int i = 1; i < nCustomers + 1; i++) {
seats.submit(new Customer("Customer " + i, pizzas));
}

// Knock off when all customers are gone
seats.shutdown();
}

private static class Pizza {

private String name;

public Pizza() {}

public Pizza(final String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

private static class PoisonPizza extends Pizza {

public PoisonPizza() {
super("PoisonPizza");
}

}

private static class Customer implements Runnable {

private String name;

private BlockingQueue<Pizza> pizzas;

public Customer() {}

public Customer(final String name, final BlockingQueue<Pizza> pizzas) {
this.name = name;
this.pizzas = pizzas;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public BlockingQueue<Pizza> getPizzas() {
return pizzas;
}

public void setPizzas(final BlockingQueue<Pizza> pizzas) {
this.pizzas = pizzas;
}

@Override
public void run() {
try {
// Order a pizza
final Pizza pizza = pizzas.take();

// Leave the restaurant if all pizzas have been aten
if (pizza instanceof PoisonPizza) {
pizzas.put(pizza);
System.out.println(name + ": I'm so hungry.");

return;
}

// Eat the pizza
System.out.println(name + ": " + pizza.getName() + " was delicious!");
} catch (InterruptedException e) {
return;
}
}

}

}

关于Java线程练习: Access control to a limited resource not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32200883/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com