Using isAlive( ) and join( )

isAlive()-

This method is defined by the Thread class. when we call this method on Thread it will return true if it is running otherwise false. This will use rarely.

General form is:

final boolean isAlive( )

join()-

This method is used to wait for a thread to finish. This name specified as it calling Thread will wait until the specified Thread joins it. It will also provide the maximum time to wait for a thread for it's termination.

General form is:

final void join( ) throws InterruptedException

//example program for isAlive() and join() methods
class NewThread implements Runnable
{
 String 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 = 3; i > 0; i--) 
 {
 System.out.println(name + ": " + i);
 Thread.sleep(1000);
 }
 } 
 catch (InterruptedException e) 
 {
 System.out.println(name + " interrupted.");
 }
 System.out.println(name + " exiting.");
 }
}

public class DemoJoin 
{
 public static void main(String args[]) 
 {
 NewThread ob1 = new NewThread("One");
 NewThread ob2 = new NewThread("Two");

 System.out.println("Thread One is alive: " + ob1.t.isAlive());
 System.out.println("Thread Two is alive: " + ob2.t.isAlive());

 // wait for threads 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("Thread One is alive: " + ob1.t.isAlive());
 System.out.println("Thread Two is alive: " + ob2.t.isAlive());
  System.out.println("Main thread exiting.");
 }
}

Output:

New thread: Thread[One,5,main]

New thread: Thread[Two,5,main]

Thread One is alive: true

Thread Two is alive: true

Two: 3

Waiting for threads to finish.

One: 3

Two: 2

One: 2

Two: 1

One: 1

Two exiting.

One exiting.

Thread One is alive: false

Thread Two is alive: false

Main thread exiting.

Using setPriority() and getPriority()

  • Thread priorities are used by the thread scheduler to decide when each thread should be allowed to run.
  • In theory, the high priority thread will get more CPU time than low priority but in practice, it depends on several factors.
  • A higher-priority thread can also preempt a lower-priority one.

  • If both threads have same priority there will be some blocking situation, such as waiting for I/O.

  • When this happens, the blocked thread is suspended and other threads can run. But, if you want smooth multi threaded execution, you are better off not relying on this.

setPriority( )-

This method is the member of Thread class which is used to set the priority for Threads.

General form is:

final void setPriority(int level)

  • level is the new priority setting for a calling Thread which is within the range of MIN_PRIORITY and MAX_PRIORITY. Currently, these values are 1 and 10, respectively.
  • To set for a default priority we have to specify NORM_PRIORITY, which is currently 5.
  • These priorities are defined as static final variables within Thread means we can't change those values.

getPriority( )-

This method is the member of Thread class which is used to obtain the current priority of the Thread which calls this method.

General form is:

final int getPriority( )

results matching ""

    No results matching ""