基于Prority的线程? [英] Prority based threads?
问题描述
我正在研究线程优先级,并且我同时拥有Windows 10和Ubuntu 16.0 lts操作系统.
I'm studying thread priorities and I have both windows 10 and Ubuntu 16.0 lts operating system.
我知道Windows不提供基于优先级的处理,因此我看不到使用基于优先级的线程程序的实际工作原理.
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 .
所以我在ubuntu中运行基于优先级的程序,因为有人告诉我ubuntu提供了基于优先级的进程.
So I run my priority based program in ubuntu because someone told me that ubuntu provide priority based process.
但是当我运行程序时,它显示的输出与Windows相同或混合.
but when i run my program it show the same output or mixed output as windows.
所以有什么方法可以在ubuntu中启用优先级,我们可以在Windows中启用优先级,但是为此,我应该拥有真正的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);
}
}
推荐答案
在Java中,线程 priorities 更像是对JVM的推荐.
In Java, thread priorities are more of an recommendation towards the JVM.
换句话说:每个JVM实现都自行决定如何利用此概念.因此,这不仅涉及Windows与Linux.还有关于Oracle JVM与OpenJDK或IBM JVM的关系;可能还涉及一个JVM的版本A和该版本的一些较新版本B.
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.
此外,还要:确保这样一个(很抱歉)天真,意味着过于简单的示例代码片段将不可以帮助您确定JVM是否支持优先级.
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.
重点是:系统在重负载下不时;而且资源也不短缺...甚至有一个低优先级的线程也有可能以与高优先级线程相同的速率吐出println语句.
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!
这篇关于基于Prority的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!