我知道当前使用Executors而不是ThreadGroup的做法:
然而,ThreadGroup本身的固有缺陷是什么(我听过那个类的含糊不清的批评)?
谢谢你的回答.
PS.这似乎没有回答这个问题.
任何人都可以解释线程优先级如何在java中工作.这里的困惑是,如果java不保证Thread根据其优先级的实现,那么为什么这个setpriority()函数用于.
我的代码如下:
public class ThreadSynchronization implements Runnable{
public synchronized void run() {
System.out.println("Starting Implementation of Thread "+Thread.currentThread().getName());
for(int i=0;i<10;i++)
{
System.out.println("Thread "+Thread.currentThread().getName()+" value : "+i);
}
System.out.println("Ending Implementation of Thread "+Thread.currentThread().getName());
}
public static void main(String[] args) {
System.out.println("Program starts...");
ThreadSynchronization th1 = new ThreadSynchronization();
Thread t1 = new Thread(th1);
t1.setPriority(1);
synchronized(t1)
{
t1.start();
}
ThreadSynchronization th2 = new ThreadSynchronization();
Thread t2 = new Thread(th2);
t2.setPriority(9);
synchronized (t2) {
t2.start();
}
System.out.println("Program ends...");
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的程序中,即使我改变优先级,我发现输出没有区别.另外,如何使用线程优先级的实时应用将会有很大帮助.谢谢.