Interrupting a thread waiting for a blocking action?

I start a thread, the main action of which is to call a proxy server using the blocking function, and wait until it gives something.

I used the famous volatile boolean and interrupt pattern, but I'm not sure if this will work: when I try to add a catch block for InterruptedException , I get an error:

Unreachable catch block for InterruptedException. This exception is never thrown from the body of a try statement.

So, if I never get an InterruptedException , it means that I will never exit the blocking action - thus, it will never stop.

I am a little puzzled. Any idea?

  public void run() { Proxy proxy = ProxyFactory.generateProxy(); Source source; while (!isStopped) { try { source = proxy.getPendingSources(); scheduleSource(source); } catch (Exception e) { log.error("UnExpected Exception caught while running",e); } } } public void stop() { this.isStopped = true; Thread.currentThread().interrupt(); } 
+4
source share
5 answers

First, you do not need a separate flag (if you do, use AtomicBoolean ), just mark Thread.currentThread().isInterrupted() as your condition.

Secondly, your stop method will not work because it will not interrupt the correct thread. If another thread causes a stop, the code uses Thread.currentThread() , which means that the calling thread will be interrupted and not working.

Finally, what is a blocking method? Is this scheduleSource() ? If this method does not throw an InterruptedException , you cannot catch it.

Try the following:

 private final AtomicReference<Thread> currentThread = new AtomicReference<Thread>(); public void run() { Proxy proxy = ProxyFactory.generateProxy(); Source source; currentThread.set(Thread.currentThread()); while (!Thread.currentThread().isInterrupted()) { try { source = proxy.getPendingSources(); scheduleSource(source); } catch (Exception e) { log.error("UnExpected Exception caught while running", e); } } } public void stop() { currentThread.get().interrupt(); } 
+11
source

Only a few, well-defined “blocking methods” are interrupted. If the thread is interrupted, the flag is set, but nothing will happen until the thread reaches one of these well-defined breakpoints.

For example, calls to read() and write() are interrupted if they are called in threads created using the InterruptibleChannel . If Socket used as the starting point, the interrupt() call on Thread blocked in reading does not affect. Please note that if the blocking I / O operation is aborted successfully, the main channel is closed.

Another large class of intermittent operations are those performed by various class locking operations in java.util.concurrent packages. Of course, the original wait() method is also interrupted.

Lock methods can be identified by looking for throws InterruptedException in their method signatures. They should also be well documented to describe any side effects of interruption.

You can write your own intermittent method, but it should consist of intermittent lower-level operations.

+10
source

The stop method calls interrupt on the wrong thread. Thread.currentThread() is a thread that is interrupted, not interrupted.

+2
source

OK people, don't kill me for this.

I experimented with Thread.stop () for fun, to cut a thread from a blocking action, catch ThreadDeath, maintain a live thread, and move on.

Seems to work. The world does not end. But I'm just saying. You are responsible for yourself. Why am I rap?

+2
source

How do you call a stop from an executable thread?
If you call stop () from another thread, you will kill it, not the thread running in the try / catch block.

+1
source

All Articles