许多方法,如stop(),resume(),suspend()等已被弃用.
那么使用ThreadGroup?创建线程是否有用?
我想知道为什么互联网上关于线程组的文档很少?它们仍在使用,或者它们是一些陈旧的概念?有人可以解释一下:
它们是什么.
它们用于什么.
如果他们是平静的使用,在哪里?
给出一些真实的应用程序示例(可能是Web服务器).
我目前正在学习Java中的Threads基础知识,我正在尝试编写一个简单的Thread Group程序.虽然我得到了不同类型的输出,但我和教程网站一样写了它.下面是我的代码,我得到不同的输出.
public class ThreadGroupDemo implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
// get the name of the current thread.
}
public static void main(String[] args) {
ThreadGroupDemo runnable = new ThreadGroupDemo();
ThreadGroup tg1 = new ThreadGroup("Parent Group");
// Creating thread Group.
Thread t1 = new Thread(tg1, new ThreadGroupDemo(), "one");
t1.start();
t1.setPriority(Thread.MAX_PRIORITY);
Thread t2 = new Thread(tg1, new ThreadGroupDemo(), "second");
t2.start();
t2.setPriority(Thread.NORM_PRIORITY);
Thread t3 = new Thread(tg1, new ThreadGroupDemo(), "Three");
t3.start();
System.out.println("Thread Group name : " + tg1.getName()); …Run Code Online (Sandbox Code Playgroud)