-2

This is a code snippet.

class Thread1 extends Thread
{
 Thread1(String s)
 {
  super(s);
 }
 public void run()
 {
 for(int i=0;i<5;i++)
 System.out.println(getName());
 }
}

I have created Three different classes for three threads.

class Thread2 extends Thread
{
 Thread2(String s)
 {
 super(s);
 }
 public void run()
  {
 for(int i=0;i<10;i++)
 System.out.println(getName());
 }
}

class Thread3 extends Thread
{
 Thread3(String s)
 {
 super(s);
 }
 public void run()
 {
 for(int i=0;i<12;i++)
 System.out.println(getName());
 }
}

Simply trying to print the name of the thread as per the priority set by me.

class RunThread extends Thread
{
 public static void main(String... s)
 {
 Thread1 t1= new Thread1("T1");
 Thread2 t2= new Thread2("T2");
 Thread3 t3= new Thread3("T3");

but this threads are printing in the same random way..as they are printed when no priority is set.

 t1.setPriority(Thread.NORM_PRIORITY);
 t2.setPriority(Thread.MIN_PRIORITY);
 t3.setPriority(Thread.MAX_PRIORITY);
 }
}
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
Sri2110
  • 335
  • 2
  • 19
  • may be its a lame questiom..please help..i am new to threading. – Sri2110 Jul 25 '16 at 16:24
  • In Java Thread priority is not guaranteed. Please go through below links. You can get more details/knowledge http://stackoverflow.com/questions/12038592/java-thread-priority-has-no-effect and http://stackoverflow.com/questions/27142551/thread-priority-does-not-work-as-expected. – SamB Jul 25 '16 at 16:33
  • [Thread priority **isn't very meaningful** when all threads are **competing for CPU**.](http://www.javamex.com/tutorials/threads/priority_what.shtml) Also see http://stackoverflow.com/questions/1617963/setting-priority-to-javas-threads – azurefrog Jul 25 '16 at 16:33

2 Answers2

1

Thread priority doesn't do what you think it does

  • it is a hint to the OS which it can't ignore esp if you are not an administrator or root.
  • it only makes a difference if you have full utilisation of your CPUs. If all your CPUs are busy, it can use priority to decide how much time each one gets. If you have free CPUs every thread can run regardless of priority.
  • the time it takes to start a thread is not instant. A thread can take 0.1 ms to 10 ms to start, and in that time a thread can print many thousands of lines of output.

NOTE: Java uses native threads (in almost every modern JVM) which means it is the OS not Java which does the scheduling.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

Peter Lawrey said, "it is a hint to the OS..." I just wanted to touch on what "hint" means.

Most desktop/laptop/server/mobile-device operating systems do not allow user-mode code to directly control scheduler priorities. Those operating systems all assume that multiple, independent applications compete with one another to use the available CPUs, and their scheduling algorithms continually adjust the true priority of each thread in order to give each thread that wants it, a "fair share" of the available processing power.

Usually there is a parameter that user-mode code can adjust that scales the size of a thread's share. (In Unix, it's called the thread's "nice" value.) That's typically the only knob that Java can turn.

Some operating systems have real-time features that are appropriate to use when all of the threads on the box cooperate with one another instead of competing for resources. Those features typically include a choice of different scheduling policies, usually including at least one that allows user-mode code to directly control the true scheduling priorities.

A real-time edition of Java can take advantage of those features.

https://en.wikipedia.org/wiki/Real_time_Java

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57