在主线程中,用户可以在选择获取订单的选项后从控制台发出订单。选择该选项后,我将触发一个线程cook.start();负责从队列中吸引客户并准备他们的订单。准备好订单后,我就有了从线程调用的侦听器,并且侦听器仅显示消息。
问题是,即使我的代码循环后,我的主线程也已死了,我无法提供进一步的输入来要求其他命令。
public class Driver {
private static LinkedBlockingQueue<Customer> waitingCustomer=new LinkedBlockingQueue<Customer>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
OrderPrepared op=new OrderPrepared(waitingCustomer);
while (true) {
System.out.println("would you like to place new Order Press Y or N");
String input = sc.nextLine();
if ("y".equalsIgnoreCase(input)) {
Customer customer = null;
System.out.println("Enter Customer Name");
String name = sc.nextLine();
System.out.println("What would you like to order");
System.out.println(" ");
System.out.println("Our Menu Card ");
System.out.println(" ");
System.out.println("Item Name\t\t" + " Price\t\t" + "Press to order");
System.out.println(" ");
System.out.println("Cheese And Tomato\t\t" + "60\t\t" + "1");
System.out.println("Margerita \t\t" + "40\t\t" + "2");
System.out.println("Burger \t\t" + "50\t\t" + "3");
System.out.println("Chicken Fried \t\t" + "90\t\t" + "4");
System.out.println(" ");
System.out.println("Please Press Number to place order");
int number=sc.nextInt();
Food food=null;
switch(number) {
case 1:
food=new Food("Cheese And Tomato", 60.0d);
case 2:
food=new Food("Margerita", 40.0d);
case 3:
food=new Food("Burger", 50.0d);
case 4:
food=new Food("Chicken Fried", 90.0d);
}
Random ran=new Random(709);
customer=new Customer(name,new Order(ran.nextInt(), food));
waitingCustomer.add(customer);
Chef chef=new Chef();
chef.setChefListener(op);
chef.setCustomers(waitingCustomer);
chef.setFood(food);
chef.start();
} else {
break;
}
}
}
}
public class Chef extends Thread {
Food food;
ChefListener chefListener;
LinkedBlockingQueue<Customer> customers;
public Chef() {
}
public Chef(Food piza, ChefListener chefListener, LinkedBlockingQueue<Customer> customers) {
super();
this.food = piza;
this.chefListener = chefListener;
this.customers = customers;
}
@Override
public void run() {
// TODO Auto-generated method stub
for (Customer customer : customers) {
chefListener.pizzaCooked(customer, food);
customers.remove();
System.out.println("customer has been removed from queue");
}
}
public Food getFood() {
return food;
}
public void setFood(Food food) {
this.food = food;
}
public ChefListener getChefListener() {
return chefListener;
}
public void setChefListener(ChefListener chefListener) {
this.chefListener = chefListener;
}
public LinkedBlockingQueue<Customer> getCustomers() {
return customers;
}
public void setCustomers(LinkedBlockingQueue<Customer> customers) {
this.customers = customers;
}
}
public class OrderPrepared implements ChefListener{
Logger logger=Logger.getLogger("OrderPrepared");
public OrderPrepared(LinkedBlockingQueue<Customer> waitingCustomer) {
}
@Override
public void pizzaCooked(Customer customer, Food food) {
// TODO Auto-generated method stub
logger.info(customer.getName()+" "+food.getName() + " has been cooked. Enjoy");
}
}
Output
import java.util.concurrent.LinkedBlockingQueue;
import java.util.logging.Logger;
public class OrderPrepared implements ChefListener{
Logger logger=Logger.getLogger("OrderPrepared");
public OrderPrepared(LinkedBlockingQueue<Customer> waitingCustomer) {
// TODO Auto-generated constructor stub
}
@Override
public void pizzaCooked(Customer customer, Food food) {
// TODO Auto-generated method stub
logger.info(customer.getName()+" "+food.getName() + " has been cooked. Enjoy");
}
}
Output
would you like to place new Order Press Y or N
Y
Enter Customer Name
Kalis
What would you like to order
Our Menu Card
Item Name Price Press to order
Cheese And Tomato 60 1
Margerita 40 2
Burger 50 3
Chicken Fried 90 4
Please Press Number to place order
2
would you like to place new Order Press Y or N
customer has been removed from queue
Nov 12, 2019 2:32:45 AM OrderPrepared pizzaCooked
INFO: Kalis Chicken Fried has been cooked. Enjoy
答案 0 :(得分:2)
nextInt()不消耗LF。在nextInt()之后跟随nextLine(),以便使用行终止符(lf)。没有这个,循环顶部的nextLine将以空行退出。
另请参见Scanner is skipping nextLine() after using next() or nextFoo()?