java - Stoping and starting a new thread after timer expires -
This is my first post overflow stack and obviously I do not have much knowledge about thread. It has a console app and thing, when the main method starts, the timer starts and my program is implemented, it includes getting information from the keyboard and it works very well.
After the timer expires, 20 seconds, it shows the call run () from the reminder task and shows the number of points that I am gathering during that time. Now after that I myself Want to make the app, the main method to start or not start the option again.
Talk My main will still gather information from the keyboard which is still executed and not running by the input method which I want. If I have put System.exit (0), of course I can not start a new thread after that. How do I pass it, in my time, there is a way to say "unless my timer expired"? or something else?
Fixed class reminder tickrask {Public runs zero () {Four answers; System.out.println ("Time has expired!" + Your result); / * System.exit (0); Thread t = new thread (); T.start (); * / System.out.println ("Do you release? Y / n"); Scanner = new scanner (System.in); Answer = in.nextLine (). Fourat (0); If (answer == 'Y') {System.out.println ("continue the program"); String [] args = {}; Main.main (args); } And System.out.println ("pause program"); System.exit (0); // prevents AWT thread (and everything else)}} static int your result; Public static zero main (string [] args) {int seconds = 20; Timer = new timer (); Timer Scheduling (new remindtask (), second * 1000); Boolean constant = true; While (continuous == true) {// .... a bunch of code ... // scanner = new scanner (system.); .. // ..... a bunch code ...}
one The short answer to how to stop the thread is to prevent threading is not a trivial task: one does not have to stop the thread from within another thread; Instead, ask stopping the thread yourself, the reason is that one thread has to be stopped at a safe place. If the thread has been closed, then keeps the mute, for example, if another thread requires this Mute X, then a deadlock can occur.
First of all, maybe it's better that you do not use the main method for keyboard voting. Just do another thread to do this; This makes a new thread easier to do the same thing later.
Second, what do you have to do, change your Remind Task
Thread Java in the context of the thread
object in the main thread, Code> thread.stop () , but due to this I have been disliked for the above reason
Instead, you have to ask to stop the main thread. Java provides a standard way of doing this: Call the main thread on the thread.inrrupt ()
threads . Then, in the main thread, you call the regular method Thread.interrupted ()
. If it returns true
, then it means that another thread has asked to end the current thread, and you can stop it by returning to the run ()
method You can.
Comments
Post a Comment