主线程&主线程组
主线程的名字是main,线程组为main
主线程内创建的子线程,默认所在的组是主线程所在的组。
package com.jdojo.threads;
public class DefaultThreadGroup {
public static void main(String[] args) {
// Get the current thread, which is called "main"
Thread t1 = Thread.currentThread();
// Get the thread group of the main thread
ThreadGroup tg1 = t1.getThreadGroup();
System.out.println("Current thread's name: " + t1.getName());
System.out.println("Current thread's group name: " + tg1.getName());
// Creates a new thread. Its thread group is the same that of the main thread.
Thread t2 = new Thread("my new thread");
ThreadGroup tg2 = t2.getThreadGroup();
System.out.println("New thread's name: " + t2.getName());
System.out.println("New thread's group name: " + tg2.getName());
}
}
创建线程组&指定线程的线程组
// Create a new ThreadGroup
ThreadGroup myGroup = new ThreadGroup("My Thread Group");
// Make the new thread a member of the myGroup thread group
Thread t = new Thread(myGroup, "myThreadName");
线程组的方法
当前活跃线程数activeCount()
父线程组getParent()
线程数enumerate()
中断所有线程interrupt()