aspose file tools*
The moose likes Threads and Synchronization and the fly likes Stoping multiple threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Stoping multiple threads" Watch "Stoping multiple threads" New topic
Author

Stoping multiple threads

Ivan Kesic
Ranch Hand

Joined: Feb 05, 2009
Posts: 68
When my aplication is running, i made a method that checks how many threads is currently running, and it say 5!
I want to stop them all, but i cant. I made a button stop, and tryed to make on click "Thread.currentThread.interupt" and do that 5 times, But it doesnt work
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

Interrupting a thread won't do anything really. It will interrupt calls like wait(), sleep() and basically all other methods that throw an InterruptedException, but the main thread will still continue. That's why the isInterrupted() method is there - you should check it and exit the run() method if it returns true.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ivan Kesic
Ranch Hand

Joined: Feb 05, 2009
Posts: 68
Rob Prime wrote:Interrupting a thread won't do anything really. It will interrupt calls like wait(), sleep() and basically all other methods that throw an InterruptedException, but the main thread will still continue. That's why the isInterrupted() method is there - you should check it and exit the run() method if it returns true.

How do i check main thread? I dont even know which is it.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Ivan wrote: How do i check main thread? I dont even know which is it.

You can not interrupt a thread without knowing it!
You are doing a Thread.currentThread.interupt(), this will always be the current thread. Unless you are doing this in different pieces of codes that are invoked in different threads, you will end up interrupting the same thread 5 time
I do not think you are familiar with multi-threading. Did you happen to go through the java concurrency tutorial? It is a nice place to start.


apigee, a better way to API!
Ivan Kesic
Ranch Hand

Joined: Feb 05, 2009
Posts: 68
I will read that tutorial, then try to do this! Thank you for help
Alan Mehio
Ranch Hand

Joined: Apr 04, 2005
Posts: 73
Ivan Kesic

Just look at the sample below which posted recently in the above thread which shows how to interrupt a thread.

If you need further help, let us know


Regards,
Alan Mehio
London, UK
Ivan Kesic
Ranch Hand

Joined: Feb 05, 2009
Posts: 68
I am still not able to stop this! My loop is counting, and the only thing i menaged is to stop the whole program, so it exit itself!

This is the Thread i need to terminate. But only this, so that i can put a new value and start loop again!
All that is by click on stop button.
Ivan Kesic
Ranch Hand

Joined: Feb 05, 2009
Posts: 68
Anybody?

Here is how i see this! When i hit start button, this loop (thread starts) and it is a current thread! I made a stop button that when i click it, does: Thread.currentThread.interupt;
So i should interupt only this loop! But nothing happen. It just continue counting down. I tried something like 1st answer at this post said, but if i do something with run() it always terminate the whole aplication! Like i wrote System.exit(0);
I just need to terminate loop, and the rest of interface should continue working!
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Ivan Kesic wrote:Anybody?

Here is how i see this! When i hit start button, this loop (thread starts) and it is a current thread!


There is your primary mistake. When you press the start button, you are in the Event Dispatch Thread. When you start the loop a new Thread starts. That new Thread will be the 'current Thread' for any code explicitly inside the run() method, but will not be the current Thread for any code that is not in the run() method.

I made a stop button that when i click it, does: Thread.currentThread.interupt;


When you hit the stop button, the code that gets called is again in the Event Dispatch Thread. So when ou do Thread.currentThread().interrupt() you would be interrupting the Event Dispatch Thread that you User Interface uses, and not your thread that does the looping.

You should read up on Concurrency in Swing.

You need to store a reference to the Thread you create when you press the Start button so you can access it when you press the Stop button. A basic example would be:


Steve
Ivan Kesic
Ranch Hand

Joined: Feb 05, 2009
Posts: 68
while (broj>=0 && keepRunning)

^^Great thing!
Thank you, now i get it!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stoping multiple threads