Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Multithreading. No Comments on Thread Priorities
0
(0)

In this article we will learn how to work with thread priorities when there are several threads competing for CPU time. Example code is also provided.

 

In a uni-processor system, when several threads are competing for the CPU, you might want a certain thread to get more CPU time (burst time) over the remaining threads. We can use thread priorities in such situation.

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


The thread class provides three final static variables (constants) namely: MIN_PRIORITY, NORM_PRIORITY, and MAX_PRIORITY whose values are 1, 5 and 10 respectively. Priority values can be in the range 1 to 10. 1 denotes minimum priority and 10 denotes maximum priority.

 

A higher priority thread always gets more CPU time over the lesser priority thread. When a lesser priority thread is currently running on a CPU and a higher priority thread resumes (after wait or sleep or unblocked from I/O), it will preempt the lesser priority thread.

 

By default all the newly created threads will have a priority of 5. For assigning a new priority to the thread, we can use setPriority() method of Thread class whose syntax is as follows:

 

final void setPriority(int priority_level)

 

For retrieving the priority of the thread we can use getPriority() method of Thread class whose syntax is as follows:

 

final int getPriority()

 

Below program demonstrates the use of setPriority() and getPriority() methods:

class MyThread1 implements Runnable
{
	Thread t;
	MyThread1(String name)
	{
		t = new Thread(this, name);
		System.out.println("Child thread: " + t);
		t.start();
	}

	@Override
	public void run()
	{
		try
		{
			for(int i = 1; i <= 10; i++)
			{
				System.out.println(t.getName() + ": " + i);
				Thread.sleep(100);
			}
		}
		catch(InterruptedException e)
		{
			System.out.println(t.getName() + " is interrupted!");
		}
		System.out.println(t.getName() + " is terminated");
	}
}

class MyThread2 implements Runnable
{
	Thread t;
	MyThread2(String name)
	{
		t = new Thread(this, name);
		System.out.println("Child thread: " + t);
		t.start();
	}

	@Override
	public void run()
	{
		try
		{
			for(int i = 1; i <= 10; i++)
			{
				System.out.println(t.getName() + ": " + i);
				Thread.sleep(500);
			}
		}
		catch(InterruptedException e)
		{
			System.out.println(t.getName() + " is interrupted!");
		}
		System.out.println(t.getName() + " is terminated");
	}
}

public class Driver
{	
	public static void main(String[] args)
	{
		MyThread1 thread1 = new MyThread1("First Thread");
		MyThread2 thread2 = new MyThread2("Second Thread");
		thread1.t.setPriority(10);
		thread2.t.setPriority(1);
		System.out.println("Priority of first thread is: " + thread1.t.getPriority());
		System.out.println("Priority of second thread is: " + thread2.t.getPriority());
		try
		{
			System.out.println("Main thread is waiting...");			
			thread1.t.join();
			thread2.t.join();
		}
		catch(InterruptedException e)
		{
			System.out.println("Main thread is interrupted!");
		}
		System.out.println("Main thread terminated");
	}
}

 

Output of the above program is:

Child thread: Thread[First Thread,5,main]
First Thread: 1
Child thread: Thread[Second Thread,5,main]
Priority of first thread is: 10
Priority of second thread is: 1
Main thread is waiting…
Second Thread: 1
First Thread: 2
First Thread: 3
First Thread: 4
First Thread: 5
First Thread: 6
Second Thread: 2
First Thread: 7
First Thread: 8
First Thread: 9
First Thread: 10
Second Thread: 3
First Thread is terminated
Second Thread: 4
Second Thread: 5
Second Thread: 6
Second Thread: 7
Second Thread: 8
Second Thread: 9
Second Thread: 10
Second Thread is terminated
Main thread terminated

 

Although the sleeping time of first thread is less than the second thread, in general, thread with higher priority gets more CPU time.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *