Resume interrupted thread

I want to resume the interrupted thread, please let me know some possible solutions for the same.

class RunnableDemo implements Runnable { public void run() { while(thread.isInterrupted()) { try{} catch(Exception e){ //exception caught} } } } 

If the exception is caught, the thread is interrupted, but even if the exception is caught, I want the thread to continue working, so please offer me some way to overcome this problem.

Thank you in advance

0
java multithreading
Apr 08 '14 at 12:49 on
source share
5 answers

Interrupting a stream is what you prefer to do when writing a stream. Therefore, if you do not want your thread to be interrupted, do not check the interrupted status and continue independently.

The only time you need try / catch statements (regarding thread interruption) is called when you call locking methods that throw an InterruptedException . Then you need to avoid the exception stopping your thread from working.

Of course ... you should consider whether this is suitable for yourself. Interrupting a thread is a useful thing, and you shouldn't stick to it, it can annoy users of your code.

+5
Apr 08 '14 at
source share

I wrote reusable code to get this function where a thread can pause and resume. Please find the code below. You can extend the PausableTask and override the task () method:

 public abstract class PausableTask implements Runnable{ private ExecutorService executor = Executors.newSingleThreadExecutor(); private Future<?> publisher; protected volatile int counter; private void someJob() { System.out.println("Job Done :- " + counter); } abstract void task(); @Override public void run() { while(!Thread.currentThread().interrupted()){ task(); } } public void start(){ publisher = executor.submit(this); } public void pause() { publisher.cancel(true); } public void resume() { start(); } public void stop() { executor.shutdownNow(); } } 

Hope this helps. For more information check out this link or give me a shout in the comments section. http://handling-thread.blogspot.co.uk/2012/05/pause-and-resume-thread.html

+1
Apr 08 '14 at 1:47
source share

A thread is interrupted only if someone called the interrupt () method of this thread, and not because when you started your thread, you think some other random exception was thrown.

When the threadrupted () method is called, an InterruptedException will be thrown into the thread if the thread is in the middle of a blocking operation (for example, reading IO).

When an InterruptedException is thrown, you should know that the interrupted status is cleared, so the next time you call isInterrupted () in your thread, you will get false (even if you just cauth InterruptedException)

Keep this in mind when encoding your streams. And if you don’t understand what I’m talking about, stop multithreading coding and read some books about concurrency in java.

0
Apr 08 '14 at 13:26
source share

One warning: if your thread is handling an InterruptedException while calling a third-party library, then you will not necessarily know how the library reacted to it (i.e. left the library objects in a state where it makes sense for your program to continue to use them?)

Some developers (including some library developers) mistakenly assume that the interrupt means "turn off the program," and all they worry about is closing files, etc .; and not so much about whether you can continue to use the library.

Try it and see, but if you are writing code to control a spaceship or a nuclear reactor or something like that, you may need a little extra work to really find out what the library does.

0
Apr 08 '14 at 13:35 on
source share

As already noted, an interrupt is usually the right way to cancel a task. If you really need to complete the task without canceling, at least be sure to restore the interrupted state of the thread when you are done with your uninterrupted work:

 public void run() { boolean interrupted = false; try { while (true) { try { callInterruptibleMethod(); } catch (InterruptedException ex) { interrupted = true; // fall through and retry } } } finally { if (interrupted) { // restore interruption state Thread.currentThread().interrupt(); } } } 

(From book: Java Concurrency in Practice )

0
Apr 08 '14 at
source share



All Articles