Hi. The code below is to play a song but needs to stop when the user types the number "1." How do you make it so the public Reminder(int seconds) will stop which will then make the song stop? After it stops, I want to be able to do other things so I don't want the program to end.
class RemindTask extends TimerTask { public void run() { System.out.println("Time's up!"); timer.cancel(); //Terminate the timer thread int a; ConsoleIO console = new ConsoleIO();
System.out.print("Would you choose 1 or 2: "); a = console.readInt(); if (a == 1) Thread.currentThread().interrupt();//Need help here...
System.out.println("About to schedule task."); new Reminder(0); System.out.println("Task scheduled.");
} }
Yaroslav Chinskiy
Ranch Hand
Joined: Jan 09, 2001
Posts: 147
posted
0
You can put riminder on a separate thread. Also it looks like you want it to be a singleton. In the TimerTask, use reference to interrupt the thread (not currentThread()) also in the while loop use volatile boolean run and implement abort() method that will set run=false;
volatile boolean run = true;
public void abort(){ run = false; }
while(nBytesRead != -1 && run){ ... }
Also, if you are going to make it part of GUI, use KeyEvent and KeyEventListener to trap pressed keys.