Thursday, April 29, 2010

How to Stop a Thread

There is no direct method to stop a thread. Earlier there used to be methods like Thread.stop(), however now these are deprecated. So how can we stop a thread if there is no 'stop' method?

To understand the answer, we first need to understand the thread and its working. For understanding the thread, please refer to other post 'What is Thread'. So one thread performs a assigned work at a time and its life is over once this work is done. Like if we take example quoted in 'What is Thread' post, then a painting work assigned to a sub-contractor is the life of relation with him. As soon as, he will complete his painting work, the sub-contracting relation will end. 

So the right way to stop a thread is to complete its work, or to make it understand that its work is over and it has to exit. For example, Painting contractor can ask the sub-contractor painter while assigning the job that please work till you finish painting in this service or till I call you to come out of this room. So the sub-contractor will start painting and will automatically exit from the relation as soon as it will finish the painting. Alternatively Painting Contractor can call him in-between and so the sub-contractor will keep on checking that whether the contractor is calling him or not. If not, he will continue with his work till its completion. If yes, he will leave the work and exit the room. 


Same code: 









// Job class representing the Sub-Contractor Painter
public class SubContractorPainter implements Runnable {
/**
* It contains the status that whether the painting contractor has called
* the sub-contractor or not. 
*/
private boolean calledByPaintingContractor;

public boolean isCalledByPaintingContractor() {
return calledByPaintingContractor;
}

/**
* This method can be called by contractor any time whenever he wants to stop
* the sub-contractor from doing the painting work
* @param calledByPaintingContractor true if Painting Contractor gives a 
* call to the sub-contractor
*/
public void setCalledByPaintingContractor( boolean calledByPaintingContractor ) {
this.calledByPaintingContractor = calledByPaintingContractor;
}

public void run() {
// work till contractor is not calling
while (!isCalledByPaintingContractor())
{
// keep doing work for next 1 unit of time
// if work is done, break;
}


Thread.stop() method has been deprecated because it is not safe to kill a thread abruptly when it is in-between of any execution, as system has not idea that what is the stage of execution for this thread. So if we are stopping the thread by giving an external command, it MAY leave the system in inconsistent state. So great inventors found it reasonable, if thread itself takes the responsibility to stop and makes the provision for early exit.

Stopping a thread abruptly is same like if we drag a painter out of the room without giving him a chance to exit. And the result is that his paint can splash over the walls and floor and ruin the completed work also.

1 comment:

  1. Its a excellent blog sir Now today I clear the Tread concept whole with the help of this real life example.

    ReplyDelete