Search code examples
javatimeterminate

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(). In this method he selects number of tickets and then I want to give him 10 minutes to select seats. If he select them and he confirm the order, then the method should end and he can again select option from MainMenu. If he doesnt select seats in 10 minutes I want to delete order, inform him about it and show him 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();
}
}

Solution

  • As I understood you want to wait for 10 minutes for user action. If he/she actions within 10 minutes then you proceed to confirm the order.

    If the user failed to take action within 10 minutes you want to delete the order.

    You can try Future which waits for a thread to complete at a specific time.

    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();
            }