【Java】【Thread多线程】线程组

线程组

public class Demo {
    public static void main(String[] args) throws IOException, InterruptedException {
        ThreadGroup tg = new ThreadGroup("新线程组");
        MyRunnable mr = new MyRunnable();
        
        Thread t1 = new Thread(tg, mr, "张三");
        Thread t2 = new Thread(tg, mr, "李四");
        
        System.out.println(t1.getThreadGroup().getName());
        System.out.println(t2.getThreadGroup().getName());
    }
}

class MyRunnable implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + "......" + i);
        }
    }
}

你可能感兴趣的:(【Java】【Thread多线程】线程组)