• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to interrupt a Thread in Java  RSS feed

 
Kevin Olome
Ranch Hand
Posts: 44
1
Android Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, i know how to interrupt a thread in java but somehow it is not working.

My Application looks like this





Why is this not working?
I always read that interrupting a Thread is so much better than the .stop method but somehow the interrupt method don't work?

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly you are right that the stop method should not be used. It is not safe to use and is never the correct solution.

However you seem to have a misconception of what thread interrupting does. Every Thread object has a flag tracking whether the thread has been interrupted yet. When you call the interrupt method it does nothing more than set that flag. It does not cause the thread to stop executing.

It is the job of your code to detect when the thread has been interrupted, and when it has your Thread should exit as soon as possible. This is a cooperative mechanism though. If your code does not check for interruptions or ignores interruptions then the thread will continue running.

It is also not normal for a thread to interrupt itself. Normally you will interrupt a different thread when you want it to exit.
 
Kevin Olome
Ranch Hand
Posts: 44
1
Android Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answer.
So if i use an if condition and check if it is interrupted I can continue from there.

I thought about interrupting the same thread is a bit crazy but I don't know any other way arround it.

It's a rock-scissors-paper game with a network connection.

The thread here is for handling the logic and is waiting for an input from a user lets say 1 for rock 2 for paper and 3 for scissors.
x for exiting the game.

Also the game is endless till one player says x.

So the only method I knew was to interrupt it when someone says x. Only the thread and the client knows that one player is quitting. Not the server who has initially created the Thread.

Anyhow it works now but maybe there is another way arround it?


 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general yes there would be a loop that checks the interrupted flag to determine when the thread should exit. However there are some problems with this mechanism, one of which is that if you call a blocking method from your that does not respond to thread interruptions then your thread won't exit. I believe blocking IO is one such place where interruptions aren't honoured.

However it does not look to me like thread interruptions are the right mechanism for you anyway. When your thread notices it is time to exit it should just exit.

Without knowing your requirements and design its hard to give more specific advice though.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The interrupt() method doesn't immediately cause anything in the thread to happen.

However, when the thread is currently inside some blocking call, such as Thread.sleep() or another method that might throw InterruptedException, then an InterruptedException occurs in the thread, when another thread calls interrupt() on the thread.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!