Create Lightweight Heavyweight Threads

Create Lightweight Heavyweight Threads


2. Creating Lightweight Threads

The above program is an example of heavyweight process. Let us modify the program to make it as a lightweight process. You know earlier, lightweight process means, shifting the execution control between the threads of the same process. That is here, two threads belong to the same class. It is just a modification of previous program.

Create Lightweight Heavyweight Threads

til1.setName(“Ascending”);
til2.setName(“Descending”);

In this program, a small problem arises. Two threads til1 and til2 belong to the same class and executes the same run() method. Then, how to print ascending and descending order numbers alternatively. Here, we give the names to threads, retrieve the names in the run() method and act accordingly.

With setName() method of Thread class, a name to a thread can be assigned. The name given to til1 is Ascending and til2 is Descending.

String threadName = this.getName();

We know, this refers the current object. Here, the current thread is the one that calls the run() method; it is either til1 or til2. If til1 calls the run() method, this refers til1 and becomes internally as til1.getName(). It returns Ascending. Similarly, when til2 calls, the threadName variable becomes Descending. Using setName() and getName() methods of Thread class, we can assign and retrieve the names of threads. Remaining part of the code is explained in the previous program, TwoRuns.

9 thoughts on “Create Lightweight Heavyweight Threads

  1. Surya

    sir,

    this same program am getting the error as “class test1 is public it should be declared in its own file” where am going wrong?? Could u please explain

  2. v.s.

    sir i cant understand if parent thread dies before child thread how there output changes as thread are independent to each other

        1. S. Nageswara Rao, Corporate Trainer

          See this program:

          Multithreading – join( ) Example

          join( ) method makes the thread to wait until other threads finish their task.

          Generally, threads are designed to run independently of the other. join( ) method involves two threads. The invocation of join causes the currently executing thread to suspend execution until target thread finished its execution. The following is the method signature:

          public final void join( ) throws InterruptedException

          join( ) method is overloaded. The difference between join( ) and wait( ) is join( ) method does not involve in synchronization( that is, no locking of data). wait( ) method is used by JVM in thread synchronization. Other way, join( ) and wait( ) methods are used in thread communication.
          The calling thread waits until called thread completes its execution and dies. By using join( ), data consistency can be maintained.
          The following example illustrates:

          Aim: to observe the effect of join( ) method in data inconsistency( in output ).

          class NewThread implements Runnable {
          String name ; // name of a thread
          Thread t ;
          NewThread( String threadname ) {
          name = threadname ;
          t = new Thread( this, name ) ;
          System.out.println(“New thread: ” + t ) ;
          t.start();
          }
          public void run( ) {
          try {
          for( int i = 2 ; i > 0 ; i – – ) {
          System.out.println( name + “: ” + i ) ;
          Thread.sleep(1000);
          }
          }
          catch( InterruptedException e ) {
          e.printStackTrace( ) ;
          }
          System.out.println( name + ” exiting” ) ;
          }
          }
          public class JoinDemo {
          public static void main( String args[ ] ) {
          NewThread ob1 = new NewThread( “One” ) ;
          NewThread ob2 = new NewThread( “Two” ) ;
          NewThread ob3 = new NewThread( “Three” ) ;

          System.out.println( “Thread One is alive: ” + ob1.t.isAlive( ) ) ;
          System.out.println( “Thread Two is alive: ” + ob2.t.isAlive( ) ) ;
          System.out.println( “Thread Three is alive: ” + ob3.t.isAlive( ) ) ;

          try {
          System.out.println( “Waiting for threads to finish.” ) ;
          ob1.t.join( ) ; ob2.t.join( ) ; ob3.t.join( ) ;
          }
          catch( InterruptedException e ) {
          e.printStackTrace( ) ;
          }
          System.out.println( “Thread One is alive: ” + ob1.t.isAlive( ) ) ;
          System.out.println( “Thread Two is alive: ” + ob2.t.isAlive( ) ) ;
          System.out.println( “Thread Three is alive: ” + ob3.t.isAlive( ) ) ;
          System.out.println( “Main thread is exiting” ) ;
          }
          }

          Typical output when join( ) exists:
          One: 2
          New thread: Thread[Two,5,main]
          New thread: Thread[Three,5,main]
          Two: 2
          Thread One is alive: true
          Three: 2
          Thread Two is alive: true
          Thread Three is alive: true
          Waiting for threads to finish.
          One: 1
          Two: 1
          Three: 1
          One exiting
          Two exiting
          Three exiting
          Thread One is alive: false
          Thread Two is alive: false
          Thread Three is alive: false
          Main thread is exiting

          Typical output when join( ) kept in comments:

          New thread: Thread[ One,5,main ]
          New thread: Thread[ One,5,main ]
          New thread: Thread[ Two,5,main ]
          One: 2
          Two: 2
          New thread: Thread[ Three,5,main ]
          Thread One is alive: true
          Thread Two is alive: true
          Three: 2
          Thread Three is alive: true
          Thread One is alive: true
          Thread Two is alive: true
          Thread Three is alive: true
          Main thread is exiting
          One: 1
          Two: 1
          Three: 1
          One exiting
          Two exiting
          Three exiting

          In the above example, with join( ) method, main( ) method( the calling thread of ob1, ob2 and ob3 ) waits until ob1, ob2 and ob3( called threads) executes their run method. That is why second set of isAlive( ) methods prints false. main( ) method is executed at last.

          When join( ) method is kept in comments, main( ) method exits even before ob1, ob2 and ob3 perform their tasks. When calling thread terminates first, the called threads are in a state of inconsistency and prints erratic results. In the above example, both sets of isAlive( ) method prints true.

Leave a Reply

Your email address will not be published. Required fields are marked *