ThreadGroup用来表示线程组,他可以对一批线程进行分类管理。如果一个线程没有显示指定线程组,则该线程属于默认线程组。在默认情况下,父线程和子线程属于同一线程组。线程一旦指定线程组即不可以改变。

class MyThread extends Thread {
    public MyThread(String name) {
        super(name);
    }

    public MyThread(ThreadGroup group, String name) {
        super(group, name);
    }

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println(getName() + "variable i:" + i);
        }
    }
}

public class ThreadGroupTest {
    public static void main(String[] args) {
        ThreadGroup mainGroup = Thread.currentThread().getThreadGroup();
        System.out.println("main thread group:" + mainGroup.getName());
        System.out.println("Is main thread  daemon?" + mainGroup.isDaemon());
        ThreadGroup tg = new ThreadGroup("new thread group");
        tg.setDaemon(true);
        System.out.println("Is tg thread group daemon?" + tg.isDaemon());
        MyThread tt = new MyThread(tg, "A thread in tg thread group");
        tt.start();
        new MyThread(tg, "B thread in tg thread group").start();

    }
}