简体   繁体   English

Java中定时器的使用

[英]Use of Timer in Java

I am writing a ticket booking system for a movie theater.我正在为电影院编写票务预订系统。 I want to start a timer that gives users 5 minutes to checkout after they've selected a seat.我想启动一个计时器,让用户在选择座位后有 5 分钟的时间结帐。 I decided to use java's Timer class.我决定使用 java 的 Timer class。

Here is the code that starts the timer, and the expiredCheckout() function that gets called:这是启动计时器的代码,以及被调用的expiredCheckout() function:

private void expiredCheckout() {
        Scanner inputObj = new Scanner(System.in);
        while (true) {
            System.out.println("Your session has expired!");
            System.out.println("If you want to start over, press 1"); 
            System.out.println("If you want to cancel, press q");
            String input = inputObj.next();
            System.out.println("made it here");
            if (input.equals("q"))
                return;
            else if (input.equals("1")) {
                this.runTheatreUI();
                break;
            } else {
                System.out.println("Please enter valid choice!");
            }
        }
    }

private void startTimer(ArrayList<Seat> seatsSelected) {
        Timer timer = new Timer();
        timer.schedule(
            new TimerTask() {
                public void run() {
                    expiredCheckout();
                    return;
                }
            }
            ,5*60*1000);
        ArrayList<Object> checkoutInfo = this.checkout();
        timer.cancel();
        if (checkoutInfo.get(0).equals(true)) {
            for (int i = 0; i < seatsSelected.size(); i++) {
                seatsSelected.get(i).setStatus(Seat.TAKEN);
                this.numSeatsAvailable += 1;
            }
        } else {
            // the user quit 
            for (int i = 0; i < seatsSelected.size(); i++) {
                seatsSelected.get(i).setStatus(Seat.OPEN);
                this.numSeatsAvailable += 1;
            }
        }
        return; 
    }

As you can see, I added print statements after reading input from the user.如您所见,我在读取用户输入后添加了打印语句。 However, the function never makes it there.然而,function 从未成功过。 When I enter a character in the terminal, there is no action.当我在终端中输入一个字符时,没有任何动作。 I'm confused as to why that happens.我很困惑为什么会这样。

To cancel the timer, you can do this within the TimerTask itself要取消计时器,您可以在 TimerTask 本身中执行此操作

            private void expiredCheckout() {
                System.out.println("in expired method");
                Scanner inputObj = new Scanner(System.in);
                while (true) {
                    System.out.println("Your session has expired!");
                    System.out.println("If you want to start over, press 1"); 
                    System.out.println("If you want to cancel, press q");
                    String input = inputObj.nextLine();
                    System.out.println("made it here");
                    if (input.equals("q")) {
                        System.out.println("quitting");
                        timer.cancel();
                        timer.purge();
                        return;
                    }
                    else if (input.equals("1")) {
                        System.out.println("Entered 1");
                        break;
                    } else {
                        System.out.println("Please enter valid choice!");
                    }
                }                   
            }

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

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