wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Threads and Infinite Loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads and Infinite Loops" Watch "Threads and Infinite Loops" New topic
Author

Threads and Infinite Loops

Partheban Udayakumar
Ranch Hand

Joined: Jul 04, 2013
Posts: 204

Hi,

I have a thread which contains an infinite loop. I have a UI which has start and stop buttons. When I click start, the thread should run and when I click stop, the thread should stop. The model of code is as given below


Thread:


UI class:



In this code, if I click stop button and then again click start button, IllegalThreadStateException occurs on t.start() line. This means the thread has not yet stopped but I want the thread to stop. How can this be solved?


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
--- Martin Fowler
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1000
    
  16
Why are you starting the same thread again? You know that you cannot start a thread more than once. Doing so would result in an IllegalThreadStateException.

Oracle Docs say the following.

public void start()

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).

It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.

Throws:
IllegalThreadStateException - if the thread was already started.
See Also:
run(), stop()

Partheban Udayakumar
Ranch Hand

Joined: Jul 04, 2013
Posts: 204

Chan Ag,

Thanks for your reply. I know that IllegalThreadStateException occurs if we start a thread that is running. I want to know how to stop it on click of stop button. t.stop() or t.interrupt() aren't working.

As you see I have a controlling variable for the while() loop. If the thread runs continuously, I can use the variable to start and stop but I fetched a value at the start of the thread which becomes null if I continuously run the thread.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You seem to misunderstand what the word "stop" means when applied to a Thread. It means that the code in the Thread's run() method terminates and the Thread can no longer be used. Likewise you're misusing "start" -- that means that the code in the Thread's run() method starts at the beginning. As already mentioned, you can only start a Thread once.

If I'm not mistaken you want to pause the Thread's operation, i.e. for it to cease what it's doing in that infinite loop. And then later you want to resume that processing.

I don't know why you aren't using a Timer to do this repetitive processing, but if you really need to use a low-level tool like Thread for this then your code should look something like this:



where paused is a boolean variable which you set on to pause and off to resume.
Partheban Udayakumar
Ranch Hand

Joined: Jul 04, 2013
Posts: 204

Paul,

Thanks for the reply. I was doing some thinking and I got the pause concept and now you have posted. I am implementing it now in my code. Thank you anyway for your time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads and Infinite Loops
 
Similar Threads
Stopping a Thread (where I don't have control of run) Help needed
Stoping multiple threads
key dispatching timedout
Daemon thread question
On Click of Button i want to start and stop execution of a java class.