Thread Cancellation Starvation Yield



Canceling a Thread

Sometimes, the programmer may require killing a thread from any state. One example of such a situation is if the thread is taking an unusual time to complete a task which the programmer cannot bear, it is the time for him to kill the thread. This is more observed in Web servers. The Web administrator keeps a timeout period for every task user requests. When the timeout period is over, he kills the thread to save the resources of the server.

To cancel a thread, the Thread class includes a method stop(). The stop() method should be used cautiously because it is going to kill an active thread. Any lock, the thread if holding, must be cleared first and then killed; else the data is left inconsistent. For this reason, this method is deprecated. When you compile the program using stop() method, the compiler raises a deprecation warning; observe the screenshot.

The following program uses stop() method. The thread is supposed to print 10 numbers, but prints only 6 as stop() method is called after printing 6 numbers.

public class StopInfo extends Thread
{
  public static void main(String args[])
  {
    StopInfo si1 = new StopInfo();
    si1.start();   
  }
  public void run()
  {
    for( int i=0; i<11; i++)
    {
      if(i==7)
      {
        System.out.println("Sorry, I could not print 7 to 10 as I am getting killed.");
        this.stop();
      }
      System.out.println("Printing: " + i);
    } 
  }
}

Thread Cancellation Starvation Yield

The following statement kills the thread when the counter i is 7. When killed, it does not print 7 to 10;

this.stop();

The println() method before the above statement is printed as it is called before killing the thread. Observe the screenshot for deprecation warning at compilation.

Starved threads – Yielding the Processor

We know earlier, the thread scheduler is OS dependent and is a very complex algorithm, executed very frequently. Whenever, a new thread joins the pool of waiting threads or leaves the pool (when blocked or dead), the order of priorities changes and the scheduler recalculates the scheduling again.

In the rescheduling, some threads may not get a chance of processor for a long time. These threads are known as starved threads. To request the JVM to get the processor time (which may not be obliged), the programmer can use yield() method. The yield() method temporarily stops the execution of a thread for a while. When stopped, the scheduler recalculates the scheduling as the order changes. In the recalculation, a starved thread may get a change of processor time; but not guaranteed. The programmer cannot force the JVM to allocate the processor time for a starved thread (like he cannot force garbage collection to take place). It does not imply any meaning, calling yield() method, when no other thread exist for execution.

public class YieldTest extends Thread
{
  public static void main(String args[])
  {
    new YieldTest().start();  
  }
  public void run()
  {
    for( int i = 0; i < 6; i++)
    {
      System.out.println("Number: " + i);
      this.yield();
    }
  }
}

Thread Cancellation Starvation Yield

In the above code, yield() method is called for every iteration. Of course, this type of coding is uncommon; it is an example to show how to use yield() method.

this.yield();

Using yield() method number of times decreases the performance. In the above statement, this refers an object of current thread calling the run() method.

new YieldTest().start();

In the above statement, an anonymous object of YieldTest is created and start() method is called. This saves memory of an object.

Pass your comments on this tutorial "Thread Cancellation Starvation Yield" to increase the strength of the subject.

8 thoughts on “Thread Cancellation Starvation Yield”

  1. Helo sir!
    Why do you use ‘new YieldDemo().start();’ instead of
    YieldDemo y1 =new YieldDemo();
    y1.start();

    is there any different technically? will it give different output?
    pleas can you explain it clearly ?
    Thank you!

  2. Sir,i have compile the above program (i,e, 1st one) …it’s not compile….what’s problem behind that….could u explain?
    is it true..
    syntex for stop() is:

    thread_name.stop();

  3. Sir,i have compile the above program (i,e, 1st one) …it’s not compile….what’s problem behind that….could u explain?
    is it true..
    syntex for stop() is:

    .stop();

    1. Starved threads – Yielding the Processor

      The thread scheduler is a very complex algorithm executed a number of times within a small period. The scheduler recalculates the scheduling of all threads whenever a new thread joins the waiting pool of threads or exits or goes to blocked state. In its scheduling, some threads may not get the microprocessor time for a long period. These threads are called starved threads. The programmer can likely (not compulsorily) get the microprocessor time for a starved thread by calling yield() method on the running thread(a thread which is executing the run() method). The yield() method, just pauses(stops) a running thread for a while, so that the scheduler recalculates the scheduling of all the waiting threads. In the recalculation, the starved thread may get a chance (not must) of the processor time.

      When the thread yields the microprocessor, the scheduler allocates the processor time to another equal or more priority thread. If no other threads exist, the yielding of the processor does not have any effect. Following program explains.

      public class YieldDemo extends Thread
      {
      public void run()
      {
      for( int i = 0; i < 5; i++) { System.out.println("Iteration: " + i); } this.yield(); } public static void main(String args[]) { new YieldDemo().start(); } } In the above program, the yield() method is called after printing all the numbers. This allows any starved thread, if exists, to get likely a chance (we cannot force a scheduler; it is entirely OS managed, like a garbage collector which is wholly managed by JVM) of processor time.

Leave a Comment

Your email address will not be published.