Using Command Line

While dealing with more than one thread, we can set the priority or preference to a specific thread, or order of preferences as needed. In the second example, we called a thread with minimum priority, but the thread with maximum priority was executed first.

tis.setPriority(Thread.MAX_PRIORITY);
th.setPriority(Thread.MIN_PRIORITY);
th.start();  // Thread.currentThread().getPriority()); was 1
tis.start();  // Thread.currentThread().getPriority()); was 10

The thread with the highest number ran first.

  • Threads are executed as round robin manner.
  • A thread will not yield to a thread with a lower priority.

The first example resembles thread_sleep example and in the second we set thread priority.

import java.util.*;
import java.io.*;
//javac test_priority.java
public class test_priority
{
public static int n1 = 1;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("-->Welcome to thread building<--");
System.out.print("type name as worker : ");
String str = br.readLine();
String s = str + " : "+ n1++;
s = str + " : "+ n1++;
Thread tis = new thread_this_priority(s);
tis.start();
s = str + " : "+ n1++;
Thread th = new Thread(new thread_that_priority(s)) ;
th.start();
}
}
class thread_this_priority extends Thread
{
public String str;
public thread_this_priority(String str)
{ System.out.println("\tthat consturctor");
this.str = str;
}
public void run()
{
System.out.println(" --void this run--- " );
for (int i = 1; i <4 ; i++){
System.out.println("this run : " + i + " sleep " + str);
try{
Thread.sleep(100);

}
catch (InterruptedException e) {
System.out.println(e);
}
System.out.println("this run : " + i + " work " + str);
}
}
}
//extends Thread
class thread_that_priority implements Runnable
{
public String str;
public thread_that_priority(String str)
{ System.out.println("\tthat consturctor");
this.str = str;
}
public void run()
{
System.out.println(" --void that run--- " );
for (int i = 1; i <4 ; i++){
System.out.println("this run : " + i + " sleep " + str);
try{
Thread.sleep(100);

}
catch (InterruptedException e) {
System.out.println(e);
}
System.out.println("that run : " + i + " work " + str);
}
}
}

No priority

 
 

Using Eclipse

import java.util.*;
import java.io.*;
public class test
{
public static int n1 = 1;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("-->Welcome to thread building<--");
System.out.print("type name as worker : ");
String str = br.readLine();
String s = str + " : "+ n1++;
s = str + " : "+ n1++;
Thread tis = new thread_this(s);
s = str + " : "+ n1++;
Thread th = new Thread(new thread_that(s)) ;
tis.setPriority(Thread.MAX_PRIORITY);
th.setPriority(Thread.MIN_PRIORITY);
th.start();
tis.start();

}
}
class thread_this extends Thread
{
public String str;
public thread_this(String str)
{ System.out.println("\tthat consturctor");
this.str = str;
}
public void run()
{
System.out.println(" --void this run--- " + Thread.currentThread().getPriority());
for (int i = 1; i <4 ; i++){
System.out.println("this run : " + i + " sleep " + str + " <>" + Thread.currentThread().getId());
try{
Thread.sleep(10);

}
catch (InterruptedException e) {
System.out.println(e);
}
System.out.println("this run : " + i + " work " + str+ " <>" + Thread.currentThread().getId());
}
}
}
//extends Thread
class thread_that implements Runnable
{
public String str;
public thread_that(String str)
{ System.out.println("\tthat consturctor");
this.str = str;
}
public void run()
{
System.out.println(" --void that run--- " + Thread.currentThread().getPriority());
for (int i = 1; i <4 ; i++){
System.out.println("that run : " + i + " sleep " + str+ " <>" + Thread.currentThread().getId());
try{
Thread.sleep(100);
}
catch (InterruptedException e) {
System.out.println(e);
}
System.out.println("that run : " + i + " work " + str+ " <>" + Thread.currentThread().getId());
}
}
}