简体   繁体   English

如何在一段时间后终止方法

[英]How to terminate method after time

I'm trying to do a ticket booking system.我正在尝试做一个订票系统。 One of the methods that a customer can select is makeOrder().客户可以 select 的方法之一是 makeOrder()。 In this method he selects number of tickets and then I want to give him 10 minutes to select seats.在这种方法中,他选择票数,然后我想给他 10 分钟到 select 座位。 If he select them and he confirm the order, then the method should end and he can again select option from MainMenu.如果他 select 并且他确认了订单,那么该方法应该结束并且他可以再次从 MainMenu 中选择 select 选项。 If he doesnt select seats in 10 minutes I want to delete order, inform him about it and show him MainMenu.如果他在 10 分钟内没有 select 座位我想删除订单,通知他并显示 MainMenu。 Im not sure how to do it.我不知道该怎么做。 Can anyone help me, please.任何人都可以帮助我,拜托。

pseudocode

public class MainMenu{

public void handle(String option) {  
   switch (option) {
      case "1":   showScreenings(); break;
      case "2":   makeOrder(); break;
      default:    System.out.println("Unknown option"); break;
   }
}

private void makeOrder(){
   // customer selects number of tickets and then order is created
   Order order = new Order()
   
   // here I want to give a customer 10 minutes to select seats so I tried it with Timer
   // but it deletes the order but still waiting for the input of the customer
   Timer t = new java.util.Timer();
    t.schedule( new java.util.TimerTask() {
        @Override
        public void run() {
            if (order.getOrderedDateTime() == null){
                try {
                    order.delete();
                    System.out.println("Time out");

                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            t.cancel();
        }
    }, TimeUnit.MINUTES.toMillis(10));

     //code to select seats
     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     String seats = br.readline()  // if he doesnt select seats in 10 minutes I want to delete order
     
     //reserve seats and confirm order
     order.confirm();
}
}

I think what you mean with "but still waiting for the input of the customer" that you need some kind of UI feedback, so you can handle this with multiple ways, one way is you can show a dialog notifying the user of order cancellation, and refresh the activity (if possible) by using:我认为“但仍在等待客户的输入”的意思是您需要某种 UI 反馈,因此您可以通过多种方式处理此问题,一种方式是您可以显示一个对话框,通知用户订单取消,并使用以下方法刷新活动(如果可能):

finish();
startActivity(getIntent());

As I understood you want to wait for 10 minutes for user action.据我了解,您想等待 10 分钟以等待用户操作。 If he/she actions within 10 minutes then you proceed to confirm the order.如果他/她在 10 分钟内采取行动,那么您将继续确认订单。

If the user failed to take action within 10 minutes you want to delete the order.如果用户未能在 10 分钟内采取行动,您想删除该订单。

You can try Future which waits for a thread to complete at a specific time.您可以尝试 Future 等待线程在特定时间完成。

System.out.println("Wait 10 seconds to get user input");
        
        StringBuilder stringBuilder = new StringBuilder();
        ExecutorService executor = Executors.newSingleThreadExecutor();
        
        Future<?> future = executor.submit(()->{
            //code to select seats
            System.out.println("Enter Seat No: ");
             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
             try {
                 stringBuilder.append(br.readLine()); 
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        
        try {
            future.get(10, TimeUnit.SECONDS);
            System.out.println("Seat selected. Let's confirm the order");
        } catch (TimeoutException | ExecutionException e) {
            future.cancel(true);
            System.out.println("Time out Delete oder");
            // delete order
        }  finally {
            executor.shutdownNow();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM