Let's see a program for use of thread priorities :
class A extends Thread { public void run( ) { int p; for(p=1;p<=5;p++) { System.out.println("p="+p); } } } class B extends Thread { public void run( ) { int q; for(q=1;q<=5;q++) { System.out.println("q="+q); } } } class C extends Thread { public void run( ) { int r; for(r=1;r<=5;r++) { System.out.println("r="+r); } } } class Gyana { public static void main(String args[ ]) { A t1=new A( ); B t2=new B( ); C t3=new C( ); t3.setPriority(Thread.MAX_PRIORITY); t2.setPriority(2); t1.setPriority(Thread.MIN_PRIORITY); t1.start( ); t2.start( ); t3.start( ); } }
Output
r=1
r=2
r=3
r=4
r=5
p=1
p=2
p=3
p=4
p=5
q=1
q=2
q=3
q=4
q=5
In this program the output represents the effect of assigning higher priority to a thread. Although thread A started first, but due to higher priority of thread B, B have preempted it and started printing the output first. At the same time, thread C that have assigned highest priority takes control over the other two threads. The thread A is the last to execute.
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc