• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stoping multiple threads

 
Ivan Kesic
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20559
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ivan Kesic
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ivan Kesic
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will read that tutorial, then try to do this! Thank you for help
 
Alan Mehio
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ivan Kesic
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Ivan Kesic
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while (broj>=0 && keepRunning)

^^Great thing!
Thank you, now i get it!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic