Java Thread Priority


 

Java Thread Priority

In this segment of tutorial we will learn about the priority in Java Thread and its use.We will create an example for the priority and see the program behaviour.Program with priority 10 will run first.

In this segment of tutorial we will learn about the priority in Java Thread and its use.We will create an example for the priority and see the program behaviour.Program with priority 10 will run first.

  • Java Threads run with some priority
  • There are Three types of Java Thread priority
    • Normal, Maximum and Minimum
  • Priority are set by setPriority() method.


Java Thread Priority Example
public class priority implements Runnable {

	@Override
	public void run() {
		for (int x = 1; x <= 3; x++)
			System.out.println(x + " This is thread "
					+ Thread.currentThread().getName());
	}

	public static void main(String[] args) {
		Thread t1 = new Thread(new priority(), "Thread  A");
		Thread t2 = new Thread(new priority(), "Thread  B");
		Thread t3 = new Thread(new priority(), "Thread  C");
		t1.setPriority(10);
		t1.start();
		t2.start();
		t3.start();
	}
}

Output

1 This is thread Thread A 2 This is thread Thread A 3 This is thread Thread A 1 This is thread Thread C 1 This is thread Thread B 2 This is thread Thread C 3 This is thread Thread C 2 This is thread Thread B 3 This is thread Thread B

Ads