Why interrupt a thread
R Programming. React Native. Python Design Patterns. Python Pillow. Python Turtle. Verbal Ability. Interview Questions. Company Questions. Artificial Intelligence. Cloud Computing. Data Science. Angular 7. Machine Learning. Data Structures. Operating System. Computer Network. Compiler Design. Computer Organization. When using the Executor framework, you can interrupt a specific task without shutting down the ExecutorService.
You may call the cancel method on that instance to interrupt the task. In situations when you service a web request by running parallel tasks, this method of cancelling tasks and not shutting down the service helps in re-using the service across multiple requests. In such situations you may want to shutdown the service only on shutdown of your web application.
Calling the cancel with true causes the task to be interrupted. The Executor framework is a complete asynchronous task execution framework.
If you have not explored it yet, I request to you to read about it. It will be a great addition to your development toolbox. I had left out the detail till now to avoid confusion. Before a blocking code throws an InterruptedException, it marks the interruption status as false.
Thus, when handling of the InterruptedException is done, you should also preserve the interruption status by calling Thread. In the task that is submitted to the ExecutorService, the printNumbers method is called twice. When the task is interrupted by a call to shutdownNow , the first call to the method finishes early and then the execution reaches the second call.
The interruption is called by the main thread only once. The interruption is communicated to the second execution of the printNumber method by the call to Thread. Hence the second execution also finishes early just after printing the first number.
Not preserving the interruption status would have caused the second execution of the method to run fully for 9 seconds. Published at DZone with permission of Praveer Gupta. See the original article here. Thanks for visiting DZone today,. Edit Profile. Sign Out View Profile. Over 2 million developers have joined DZone. A thread which is in the sleeping or waiting state can be interrupted with the help of interrupt method of Thread class. Example : Suppose there are two threads and If one of the threads is blocked in an invocation of the wait , wait long , or wait long, int methods of the Object class, or of the join , join long , join long, int , sleep long , or sleep long, int , methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException, which gives the chance to another thread to execute the corresponding run method of another thread which results into high performance and reduces the waiting time of the threads.
Attention reader! Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. RuntimeException: Thread interrupted Interrupting a thread that works normally : In the program, there is no exception occurred during the execution of thread.
Here, interrupt only sets the interrupted flag to true, which can be used by java programmer later. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
0コメント