Working With Threads in Java

1-Thread with priority

 
Thread priority is used by the thread scheduler for determining the order of execution of threads. And you are free to set a thread's priority as needed. A higher-priority thread gets more CPU time than a lower priority one. You can use the method setPriority() of the thread class.
 
Syntax
 
final void setPriority(int level)
 
Here level is an integer value but it must be within the range of MIN_PRIORITY and MAX_PRIORITY. The value of MAX_PRIORITY is 10 and the value of MIN_PRIORITY is 1; all the threads return a default priority specified as NORM_PRIORITY and its value is 5. These are all the priority defined as static final variables in the Thread class.
 
You can also able to get the current priority setting with the help of the getPriority() method of the Thread class.
 
Syntax
 
final int getPriority( )
 
Example-
  1. class MyThread extends Thread  
  2.  {  
  3.   String name;// name of the thread  
  4.   public  MyThread(String name)  
  5.     {  
  6.     super();  
  7.     this.name = name;  
  8.      
  9.   }  
  10.  // run method implementation  
  11.   public void run()  
  12.        {  
  13.     int count = 0;  
  14.     while (true)  
  15.        {  
  16.       try  
  17.         {  
  18.         sleep(100);  
  19.         } catch (InterruptedException e)  
  20.          {  
  21.         System.out.println("thread Interrupted" );  
  22.           }  
  23.       System.out.println(name+":" + count++);  
  24.          if (count == 5)  
  25.         break;  
  26.     }  
  27.   }  
  28.    
  29. }  
  30. // tester class    
  31. public class TestPrority  
  32.   {  
  33.     public static void main(String[] args)// body of the main method  
  34.        {  
  35.       MyThread thread1 = new  MyThread("thread1");  
  36.           System.out.println("current priority of thread :"+thread1.getPriority());  
  37.       thread1.setPriority(10);  
  38.           System.out.println("after set now priority of thread :"+ thread1.getPriority());  
  39.        MyThread thread2 = new  MyThread("thread2");  
  40.           System.out.println("current priority of thread :"+ thread1.getPriority());  
  41.       thread2.setPriority(1);  
  42.           System.out.println("after set now priority of thread :"+ thread1.getPriority());  
  43.       thread2.start();  
  44.       thread1.start();  
  45.      }//main close  
  46.  
Description-In this example we set an explicit priority of both threads thread1 which has priority 10 means it has max_priority and thread2 has the min_priority so in the TestPriority class start first thread2 but execute the first thread1; see the following output carefully:
 
Output:
 
priority testcmd.jpg
 

2-Using isAlive() and join() method with thread  

 
However, this is hardly a satisfactory solution and it also raises a larger question; that is how can a thread know when another thread has ended? So the thread class provides a method isAlive(). This method helps you to determine whether a thread isAlive or not. The other method is join(). This method is finished the wait of any thread. This method waits until the thread on which it is called terminates. Its name comes from the concept of the calling thread waiting until the specified thread joins it. Additional forms of join() allow you to specify a maximum amount of time that you want to wait for a specified thread to terminate.
 
Syntax
 
final boolean isAlive()
 
the isAlive() method returns a boolean value; true when the thread is still in the running state and otherwise it returns false.
 
Syntax
 
final void join();
 
Eaxmple
  1. class MyThread implements Runnable  
  2. {  
  3.   String name; // String variable thread  
  4.   Thread t;  
  5. // defination of constructor.....  
  6. MyThread(String threadname) {  
  7.     name = threadname;  
  8.     t = new Thread(this, name);  
  9.     System.out.println("New thread: " + t);  
  10.     t.start(); // Start the thread  
  11.   }  
  12. // body of run method  
  13. public void run()  
  14.       {  
  15.       try {  
  16.        for (int i = 0; i < 5; i++) {  
  17.         System.out.println(name + ": " + i);  
  18.         Thread.sleep(2000);  
  19.       }  
  20.     } catch (InterruptedException e) {  
  21.       System.out.println(name + " interrupted.");  
  22.     }  
  23.     System.out.println(name + " exiting.");  
  24.   }  
  25. }  
  26.    
  27. class Exp_Join {  
  28.   public static void main(String args[]) {  
  29.    // creating threa ..........  
  30.        MyThread ob1 = new MyThread("One");  
  31.     MyThread ob2 = new MyThread("Two");  
  32.     MyThread ob3 = new MyThread("Three");  
  33.   // check it is is Alive or not here ans is true printed because all isAlive  
  34.     System.out.println("Thread One is alive: " + ob1.t.isAlive());  
  35.     System.out.println("Thread Two is alive: " + ob2.t.isAlive());  
  36.     System.out.println("Thread Three is alive: " + ob3.t.isAlive());  
  37.    //join() used with in try block because it throws InterruptedException exception  
  38.        try {  
  39.       System.out.println("Waiting for threads to finish.");  
  40.       ob1.t.join();  
  41.       ob2.t.join();  
  42.       ob3.t.join();  
  43.     } catch (InterruptedException e) {  
  44.       System.out.println("Main thread Interrupted");  
  45.     }  
  46. // now thread is existing so isAlive method return false  
  47.     System.out.println("Thread One is alive: " + ob1.t.isAlive());  
  48.     System.out.println("Thread Two is alive: " + ob2.t.isAlive());  
  49.     System.out.println("Thread Three is alive: " + ob3.t.isAlive());  
  50.    
  51.     System.out.println("Main thread exiting.");  
  52.   }  
  53.  
Output:
 
cmd2.jpg
 
For a detailed tutorial on MultiThreading in Java Click
 
Resource
 


Similar Articles