Object Oriented Programming: Unit III: Exception Handling and Multithreading

Suspending - Resuming and Stopping Threads

with Example Java Programs

Stopping a thread: A thread can be stopped from running further by issuing the following statement - th.stop();

Suspending - Resuming, and Stopping Threads

‘• Stopping a thread: A thread can be stopped from running further by issuing the following statement -

th.stop();

By this statement the thread enters in a dead state. From stopping state a thread can never return to a runnable state.

‘• Blocking a thread: A thread can be temporarily stopped from running. This is called blocking or suspending of a thread. Following are the ways by which thread can be blocked

1. sleep()

By sleep method a thread can be blocked for some specific time. When the specified time gets elapsed then the thread can return to a runnable state.

2. suspend

By suspend method the thread can be blocked until further request comes. When the resume() method is invoked then the thread returns to a runnable state.

3. wait

The thread can be made suspended for some specific conditions. When the notify method is called then the blocked thread returns to the runnable state.

The difference between the suspending and stopping thread is that if a thread is suspended then its execution is stopped temporarily and it can return to a runnable state. But in case, if a thread is stopped then it goes to a dead state and can never return to runnable state.

Resuming a thread: The resume() method is only used with suspend() method. This method is only to resume a thread which was suspended using suspend() method. The syntax is -

public final resume()

Ex. 3.17.1 Write a java program for inventory problem to illustrates the usage of thread synchronized keyword and inter thread communication process. They have three classes called consumer, producer and stock.

Sol.:

class Consumer implements Runnable

{

Stock obj1;

Thread t;

Consumer (Stock obj1)

{

this.obj1 = obj1;

t = new Thread(this, "Consumer thread");

t.start();

}

public void run().

{

while (true)

{

try {

t.sleep(900);

} catch (InterruptedException e) { }

obj1.getStock((int)(Math.random()*100));

}

}

void stop()

{

t.stop();

}

}

class Producer implements Runnable

{

Stock obj2;

Thread t;

Producer(Stock obj2)

{

this.obj2 = obj2;

t = new Thread(this, "Producer thread");

t.start();

}

public void run()

{

while(true)

{

try {

t.sleep(900);

} catch (InterruptedException e) { }

obj2.addStock((int)(Math.random()*100));

}

}

void stop()

{

t.stop();

}

}

class Stock

{

int items = 0;

public synchronized void addStock(int i)

{

items = items + i;

System.out.println("Stock added:" + i);

System.out.println("present stock :" + items);

notify();

}

public synchronized int getStock(int j)

{

while(true)

{

if(items >= i)

{

items = items - j;

System.out.println("Item is removed from stock :" + j);

System.out.println("Current stock is :" + items);

break;

}

else

{

System.out.println("\tStock is not enough!!!" );

System.out.println ("\t Waiting for items to get added...");

try {

wait();

}catch(InterruptedException e) { }

}

}

return items;

}

public static void main(String args[])

{

Stock j = new Stock();

Producer p = new Producer(j);

Consumer c = new Consumer(j);

try

{

Thread.sleep(10000);

p.stop();

c.stop();

p.t.join();

c.t.join();

System.out.println("Thread stopped");

} catch (InterruptedException e) { }

System.exit(0);

}

}

Object Oriented Programming: Unit III: Exception Handling and Multithreading : Tag: : with Example Java Programs - Suspending - Resuming and Stopping Threads