Threads States Assignment Help

Assignment Help: >> Thread States and Priorities - Threads States

Threads States:

Any time, suspending execution of a thread is useful. For instance, separate thread could be used to show the time of day.  Its thread can be suspended if the user doesn't need a clock. Whatever the case, suspending a thread is a easy matter. At one suspended, restarting the thread is also a simple matter.

The mechanisms to stop, suspend, and resume threads difference among Java 2 and earlier versions. While you should use the Java2 approach for all new code, you yet required to understand how these operations were accomplished for previously Java environment.  For instance you might require updating or maintaining older, legacy code. You also require understanding why a modification was made for Java2. For these reasons, the further section describes the actual way in which the execution of a thread was controlled, followed through a section which describes the new approach needed for Java2.

Prior to Java 2, a program used suspend ( ) or resume ( ), that are methods distinct through Thread, to pause and restart the execution of a thread. They have the form that are display below:

final void suspend ( )

final void resume ( )

The subsequent program demonstrates these methods.

/ / Using suspend ( ) and resume ( ).

class NewThread implements Runnable {

Straing name; / / name of thread

Thread t;

NewThread(String threadname) {

name = threadname;


t = new Thread (this , name);

System.out.println ("New thread:" + t );

t.start ( ); / / Start the thread

}

/ / This is the entry point for thread

public void run ( ) {

try {

for (int i = 15; i>0; i - -) {

System .out.println(name + ":" + i);

Thread.sleep (200);

}

} catch (InterruptedException e ) {

System.out.println (name + "interrupted.");

}

System.out.println (name + "exiting.");

}

}

class SuspendResume {

public static void main ( String args [ ]) {

NewThread ob1= new NewThread ("One");

NewThread ob2 = new NewThread ("Two");

try {

Thread.sleep (1000);

obl.t.suspend ( );

System.out.println ("Suspending thread One");

Thread.sleep (1000);

obl.t.resume ( );

System.out.println("Resuming thread one");

ob2.t.suspend( );

System.out.println("Suspending thread Two");

Thread.sleep (1000);

ob2.t.resume ( ) ;

System.out.println ("Resuming thread Tow);

}catch (InterruptedException e)  {

System.out.println("Main thread Interrupted");

}

/ / wait for thread to finish try     {

System.out.println ("Waiting for threads to finish.");

ob1.t.join ( );

ob2.t.join( );

} catch (InterruptedException e ) {

System.out.println ("Main thread Interrupted");

}

System.out.println ("Main thread exiting.");

}

}

The output from this program is displays here:

New thread: Thread [One,5,main]

One: 15

New thread: Thread[Tow,5 main] Two: 15

One : 14

Two : 14

One : 13

Two: 13

One : 12

Two : 12

One :  11

Two :  11

Suspending thread One

Two: 10

Two :  9

Two :  8

Two : 7

Two : 6

Resuming thread One

Suspending thread Two

One :   10

One :   9

One :   8

One :   7

One :   6

Resuming thread Two

Waiting for threads to finish

Two : 5

One: 5

Two : 4

One: 4

Two : 3

One :  3

Two :  2

Two :  2

Two :  1

One: 1

Stop method Suspend or resume method
suspending and resuming a thread
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd