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. // run method implementation
  10. public void run()
  11. {
  12. int count = 0;
  13. while (true)
  14. {
  15. try
  16. {
  17. sleep(100);
  18. } catch (InterruptedException e)
  19. {
  20. System.out.println("thread Interrupted" );
  21. }
  22. System.out.println(name+":" + count++);
  23. if (count == 5)
  24. break;
  25. }
  26. }
  27. }
  28. // tester class
  29. public class TestPrority
  30. {
  31. public static void main(String[] args)// body of the main method
  32. {
  33. MyThread thread1 = new MyThread("thread1");
  34. System.out.println("current priority of thread :"+thread1.getPriority());
  35. thread1.setPriority(10);
  36. System.out.println("after set now priority of thread :"+ thread1.getPriority());
  37. MyThread thread2 = new MyThread("thread2");
  38. System.out.println("current priority of thread :"+ thread1.getPriority());
  39. thread2.setPriority(1);
  40. System.out.println("after set now priority of thread :"+ thread1.getPriority());
  41. thread2.start();
  42. thread1.start();
  43. }//main close
  44. }
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. class Exp_Join {
  27. public static void main(String args[]) {
  28. // creating threa ..........
  29. MyThread ob1 = new MyThread("One");
  30. MyThread ob2 = new MyThread("Two");
  31. MyThread ob3 = new MyThread("Three");
  32. // check it is is Alive or not here ans is true printed because all isAlive
  33. System.out.println("Thread One is alive: " + ob1.t.isAlive());
  34. System.out.println("Thread Two is alive: " + ob2.t.isAlive());
  35. System.out.println("Thread Three is alive: " + ob3.t.isAlive());
  36. //join() used with in try block because it throws InterruptedException exception
  37. try {
  38. System.out.println("Waiting for threads to finish.");
  39. ob1.t.join();
  40. ob2.t.join();
  41. ob3.t.join();
  42. } catch (InterruptedException e) {
  43. System.out.println("Main thread Interrupted");
  44. }
  45. // now thread is existing so isAlive method return false
  46. System.out.println("Thread One is alive: " + ob1.t.isAlive());
  47. System.out.println("Thread Two is alive: " + ob2.t.isAlive());
  48. System.out.println("Thread Three is alive: " + ob3.t.isAlive());
  49. System.out.println("Main thread exiting.");
  50. }
  51. }
Output:
cmd2.jpg
For a detailed tutorial on MultiThreading in Java Click
Resource
Thread Locking in C#

Comments

Join the conversation! Your thoughts help the community grow.