• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using interupt to stop the Thread

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to stop the Thread after executing certain lines of code .

If I use Thread.interrupt(); , it is not stopping .
But if when i use Thread.stop() , it is stopping but as Thread.stop() is deprecated , please let me know what is the best way to stop the Thread in between .


This is my java code :




please advice , thanks in advance .
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming that that run method is the only method you're calling in the thread. You can use return. Then the method is done and so is the thread.
 
Mike Peters
Ranch Hand
Posts: 67
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
replace t.interrupt(); by break;

If you want to use interrupt, you must check the interrupted state of the thread.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your susggestions , i just want to know more about interupt method of Thread , so i am not using break or return .

If you want to use interrupt, you must check the interrupted state of the thread


I did not get you on the above statement .
Could you please tell , what does the above mean .

Actually the question what i want to ask his that what is the best and guaranteed way to stop a Threads execution completely .
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The thread itself is responsible to stop itself. You should not stop a thread in the middle of his execution cycle. That can cause a lot of problems. That's why those thread methods are deprecated. A thread can check it's state by Thread.interrupted() and should then do a proper stop. Some of the thread methods throw interruptedException and then you should do the same.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi , thnaks for the reply ,

you mean to say that we must this way :


Thread t = Thread.currentThread();
Thread.State state = t.getState();

if(state.toString().equals("Interrupted"))
{
// Then what to do here ??
}

please tell me what to do in this piece of block ?? Thanks in advance .

 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That totally depends on the situation. You should just stop executing stuff. Something like:

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Wouter , this was what i wanted actually . Thnaks once again .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!