Saturday, 25 December 2021

Thread priorities in Java

In java, each thread is assigned a priority, which affects the order in which it is scheduled for running. The threads of same priority are given equal treatment by the java scheduler and therefore they share the processor on a first come, first serve basic.

Java permits us to set the priority of a thread using the set priority methods as follows

Syntax: Thread_Name.setPriority(int number);

The int number is range from 1-10. The thread class defines three priority constants

1. MIN_PRIORITY=1

2. NORM_PRIORITY=5

3.MAX_PRIORITY=10

When a thread is currently being executed if priority thread is coming into the running state the low priority thread is yielded.

import java.lang.*;
class A extends Thread
{
	public void run()
	{
		for(int i=0;i<=5;i++)
		{
			System.out.println(" From thread A:i="+i);
		}
	 System.out.println("Exit from A");
	}
}
class B extends Thread
{
	public void run()
	{
		for(int j=0;j<=5;j++)
		{
			System.out.println(" From thread B:i="+j);
		}
	 System.out.println("Exit from B");
	}
}
class C extends Thread
{
	public void run()
	{
		for(int k=0;k<=5;k++)
		{
			System.out.println(" From thread C:i="+k);
		}
	 System.out.println("Exit from C");
	}
}
class ThreadPriorityDemo
{
	public static void main(String args[])
	{
		A a1=new A();
		B b1=new B();
		C c1=new C();
		a1.setPriority(Thread.NORM_PRIORITY);
		b1.setPriority(Thread.MIN_PRIORITY);
		c1.setPriority(Thread.MAX_PRIORITY);
		System.out.println("A thread is started");
		a1.start();
		System.out.println("B thread is started");
		b1.start();
		System.out.println("C thread is started");
		c1.start();
	}	
}

Depending upon resources, multiple threads will run here, are two instances of outputs.

Run-1:

D:\java examples>javac ThreadPriorityDemo.java

D:\java examples>java ThreadPriorityDemo
A thread is started
B thread is started
C thread is started
 From thread A:i=0
 From thread A:i=1
 From thread A:i=2
 From thread B:i=0
 From thread C:i=0
 From thread B:i=1
 From thread A:i=3
 From thread B:i=2
 From thread C:i=1
 From thread B:i=3
 From thread A:i=4
 From thread B:i=4
 From thread C:i=2
 From thread B:i=5
 From thread A:i=5
Exit from B
 From thread C:i=3
Exit from A
 From thread C:i=4
 From thread C:i=5
Exit from C

Run-2:

D:\java examples>java ThreadPriorityDemo
A thread is started
B thread is started
C thread is started
 From thread A:i=0
 From thread B:i=0
 From thread C:i=0
 From thread A:i=1
 From thread C:i=1
 From thread C:i=2
 From thread B:i=1
 From thread C:i=3
 From thread A:i=2
 From thread C:i=4
 From thread B:i=2
 From thread C:i=5
 From thread A:i=3
Exit from C
 From thread B:i=3
 From thread A:i=4
 From thread B:i=4
 From thread A:i=5
 From thread B:i=5
Exit from A
Exit from B

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.

Find Us On Facebook

python tutorial

More

C Programming

More

Java Tutorial

More

Data Structures

More

MS Office

More

Database Management

More
Top