Prority based threads?

2019-07-24 16:06发布

问题:

I'm studying thread priorities and I have both windows 10 and Ubuntu 16.0 lts operating system.

And I got to know that windows doesn't provide priority based processing so that I can't see the use of priority based thread programs that how it is actually work .

So I run my priority based program in ubuntu because someone told me that ubuntu provide priority based process.

but when i run my program it show the same output or mixed output as windows.

So is there any way to enable priority in ubuntu ,we can enable priority in windows but for that I should have genuine windows which i haven't.

class MyThread extends Threads {
  public void run() {
    for(int i=0;i<20;i++)
      System.out.println(Thread.currentThread().getName()+""+i);
    }
   }

class TestThread {
  public static void main(String[] arg){
  Thread.currentThread().setPriority(1);
  MyThread t1=new MyThread();
  t1.setPriority(10);
  t1.start();
  for(int i=0;i<20;i++) 
   system.out.println(Thread.currentThread().getName()+""+i);
 }
}

回答1:

In Java, thread priorities are more of an recommendation towards the JVM.

In other words: each JVM implementation decides for itself how to make use of this concept. So it is not only about Windows vs. Linux; but also about Oracle JVM vs. OpenJDK or IBM JVM; and potentially also about version A of one JVM and some newer version B of that.

And beyond that: be assured that such a (sorry) naive, meaning overly simple piece of example code will not help you to figure if your JVM supports priorities or not.

The point is: while system are not under heavy load; and there is no shortage of resources ... chances are that even a low priority thread will be able to spit out println statements on the same rate a s high priority thread.

If you are really curious about exploring such things; you will have to put up a more complex example; where your threads are doing some real work; and then you try to add "load" to your system - to then observe if high priority threads can make more progress than low priority ones!