Home Tutorial Java Thread Java Thread Priority

 
 

Java Thread Priority
Posted on: November 7, 2009 at 12:00 AM
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

Related Tags for Java Thread Priority:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.