Multithreading in Java
page 4 of 5
by Debjani Mallick
Feedback
Average Rating: 
Views (Total / Last 10 Days): 5632/ 170

Suspending and Resuming a Thread

Run the following code to suspend and resume a thread.

Listing 5

class myThread implements Runnable
{
  String threadName;
  Thread t;
  Public myThread(String name)
  {
    threadName = name;
    t = new Thread(threadName);
    System.out.println(“The value of name: ” + name);
    t.start();
  }
  public void run()
  {
    try
    {
      for (int i = 15; i >= 1; i--)
      {
        System.out.println(“Thread ” + t + “Count: ” + i);
        t.sleep(200);
 
      }
    }
    catch (InterruptedException e)
    {
      System.out.println("Thread Interrupted”);
    }
    System.out.println("Terminating”+t);
  }
}
 
class demo
{
  public static void main(String a[])
  {
    myThread T1 = new myThread(“First ”); myThread T2 = new myThread(“Second ”)
      ; Try
    {
      Thread.sleep(1000); T1.t.suspend(); System.out.println(“Thread ” + T1 +
        “suspended ”); Thread.sleep(1000); T1.t.resume(); System.out.println
        (“Thread ” + T1 + “resumed ”); T2.t.suspend(); System.out.println
        (“Thread ” + T2 + “suspended ”); Thread.sleep(1000); T2.t.resume();
    }
    catch (InterruptedException e)
    {
      System.out.println("Thread Interrupted”);
    }
    Try
    {
      T1.t.join(); T2.t.join();
    }
    catch (InterruptedException e)
    {
      System.out.println("Thread Interrupted”);
    }
    System.out.println(“Finishing main thread ”);
  }
}

The threads can communicate between each other by passing some messages. When multiple threads are concurrently executed, a thread can be suspended. One thread can suspend another thread. To do so, the thread must invoke suspend() method of thread class. Once suspended, the threads can be resumed also. The suspended thread can be resumed with the help of another thread by invoking resume() method. Similarly, the execution of the thread can be forcibly terminated by another thread with the help of stop() function. However, all these functions are deprecated in Java 2. Whenever a thread is suspended, it may lead to a serious problem. If a thread is using a resource and another thread is waiting for that thread to release the resource, then if the thread which has currently acquired the resource is suspended, the resource cannot be released. It may lead to a dead lock situation. Similarly, the resume() method is also ignored in Java 2 though it does not lead to any problem, but still it cannot be used without suspend() method. In Java 2, there are some other methods which when used can suspend or resume a thread. Those are wait(), notify() and notifyall(). These methods must be called through the extending class or implementing class. When the wait() method is invoked, then the thread must release the resources and suspend temporarily. The notify() method is used to restart the threads which are in waiting state (the first one in queue). The notifyall() method will awake all the threads that have been in the waiting state. The same program can be written as shown below without using the suspend() and resume() method.

Listing 6

class myThread implements Runnable
{
  String threadName;
  Thread t;
  public myThread(String name)
  {
    threadName = name;
    t = new Thread(threadName);
    flag = false;
    System.out.println(“The value of name: ” + name);
    t.start();
  }
  public void mySuspend()
  {
    flag = true;
  }
  public void myResume()
  {
    flag = false;
    t.notify();
  }
  public void run()
  {
    try
    {
      for (int i = 15; i >= 1; i--)
      {
        System.out.println(“Thread ” + t + “Count: ” + i);
        t.sleep(200);
        while (flag)
        {
          t.wait();
        }
      }
    }
    catch (InterruptedException e)
    {
      System.out.println("Thread Interrupted”);
    }
    System.out.println("Terminating”+t);
  }
}
 
class demo
{
  public static void main(String a[])
  {
    myThread T1 = new myThread(“First ”); myThread T2 = new myThread(“Second ”)
      ; Try
    {
      Thread.sleep(1000); T1.mySuspend(); System.out.println(“Thread ” + T1 +
        “suspended ”); Thread.sleep(1000); T1.myResume();
    }
    catch (InterruptedException e)
    {
      System.out.println("Thread Interrupted”);
    }
    Try
    {
      T1.t.join(); T2.t.join();
    }
    catch (InterruptedException e)
    {
      System.out.println("Thread Interrupted”);
    }
    System.out.println(“Finishing main thread ”);
  }
}

View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 1 and 1 and type the answer here:

User Comments

Title: good   
Name: sathyaraj
Date: 7/1/2008 4:52:18 AM
Comment:
we learn more about multithreading .............
Title: Thread program not working   
Name: Thread program not working
Date: 4/30/2008 6:25:37 AM
Comment:
Listing 1
Thread program not working
Title: Very Nice   
Name: Ajit
Date: 8/20/2007 3:20:09 AM
Comment:
Keep Up
Title: Nice   
Name: Jonas
Date: 8/15/2007 10:48:08 AM
Comment:
Its a good piece of writing
Title: Good   
Name: Rocky
Date: 8/14/2007 10:37:54 PM
Comment:
Keep up the good work

Product Spotlight
Product Spotlight 
Learn More
.NET Tools
asp.net shopping cart
asp.net chart control






Ads Powered by Lake Quincy Media
Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2008 ASPAlliance.com  |  Page Processed at 8/29/2008 9:53:10 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search