ThreadGroup:多个相同功能的线程可合并到一个线程组中。线程组可用于监控组里面的活动线程数,同时操作多个线程,wait,notify。等等。。
同时可为线程组和各个线程命名。
/** * Created by li on 2016/11/24. */public class DemoThread extends Thread { private final static Object object = new Object(); public static void main(String[] args) throws InterruptedException { ThreadGroup threadGroup = new ThreadGroup("myGroup"); Thread thread1 = new Thread(threadGroup, new DemoThread(), "t1"); Thread thread2 = new Thread(threadGroup, new DemoThread(), "t2"); thread1.start(); thread2.start(); System.out.println(threadGroup.activeCount()); System.out.println("主线程干的事"); Thread.sleep(5000); System.out.println(threadGroup.activeCount()); } @Override public void run() { System.out.println(Thread.currentThread().getThreadGroup().getName() + "++" + Thread.currentThread().getName()); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } }}