0

Here's my code:

public void test(){
    final Timer timer = new Timer();
    MyThread task;
    boolean EXIT = false;
        while(!EXIT) {
            .............
            switch (choice){
                case 1:
                   .............
                    break;
                case 2:
                    if (loggedUser != null){
                        MyThread task = new MyThread(loggedUser,library);
                        timer.schedule(task,0,2000);

                        while(!logOut) {
                            menu2.displayMenu();
                            int choice2 = input.readInteger("",1,9);
                            switch(choice2){
                                case 1:
                                    .........................
                                    break;
                                case 2:
                                    ............................
                                    break;
                                case 3:
                                    break;
                                case 4:
                                   .............................
                                    break;
                                case 5:
                                    .............................
                                    break;
                                case 6:
                                    ..............................
                                    break;
                                case 7:
                                    ...............................
                                    break;
                                case 8:
                                    task.cancel();
                                    timer.purge();
                                    logOut = true;
                                    break;
                                case 9:
                                    task.cancel();
                                    timer.purge();
                                    logOut = true;
                                    EXIT = true;
                                    break;
                            }
                        }
                    }
                    break;
                case 3:
                    EXIT = true;
                    break;
            }
        }

The problem is that when EXIT option is chosen, the program doesn't end. It looks like the thread is still running, even though the task is not being carried out. What should I change? The thread has to start in the loop as some conditions must be satisfied first. The program ends with no problem when I comment out the thread part.

annsmi
  • 1
  • 1
  • Your `break`-statements are only exiting the switch-case-parts, but not the loop. – csalmhof Jun 07 '21 at 11:48
  • Change `new Timer` (you missed the parens somehow) to `new Timer(true)`. This will create a daemon thread. Daemon threads do not prevent the application from terminating, but non-daemon threads (which is the default type used by Timer) do. – Michael Jun 07 '21 at 11:50
  • Did you check if you're executing case 3 and setting the value of EXIT? What does this have to do with thread? You'll probably need to create a complete example. – matt Jun 07 '21 at 11:50

0 Answers0