Analysis of java thread group operation examples
This article describes the java thread group operation as an example. Share with you for your reference, as follows:
A finishing touch
1 Java uses ThreadGroup to represent thread groups. It can classify and manage a batch of threads. Java allows programs to directly control thread groups.
2 once a thread has joined the specified thread group, the thread will always belong to the thread group until the thread dies. The thread group it belongs to cannot be changed during thread operation.
3 thread class provides the following constructors to set which thread group the newly created thread belongs to:
Thread (ThreadGroup, runnable target): create a new thread with the run method of target as the thread executor, belonging to the group thread group.
Thread (ThreadGroup, runnable target, string name): create a new thread with the run method of target as the thread execution body. The thread belongs to the group thread group and the thread name is name.
Thread (ThreadGroup, string name): creates a new thread. The new thread is named name and belongs to the group thread group.
4. The thread class does not provide a setthreadgroup method to change the thread group to which the thread belongs, but provides a getthreadgroup() method to return the thread group to which the thread belongs. The return value of getthreadgroup() method is the ThreadGroup object, which represents a thread group.
5 ThreadGroup class has the following two simple constructors to create instances.
ThreadGroup (string name): creates a new thread group with the specified thread group name.
ThreadGroup (ThreadGroup parent, string name): creates a new thread group with the specified name and the specified parent thread group.
II. Actual combat
1 code
class MyThread extends Thread { // 提供指定线程名的构造器 public MyThread(String name) { super(name); } // 提供指定线程名、线程组的构造器 public MyThread(ThreadGroup group,String name) { super(group,name); } public void run() { for (int i = 0; i < 3 ; i++ ) { Sy@R_301_2354@.out.println(getName() + " 线程的i变量" + i); } } } public class ThreadGroupTest { public static void main(String[] args) { // 获取主线程所在的线程组,这是所有线程默认的线程组 ThreadGroup mainGroup = Thread.currentThread().getThreadGroup(); Sy@R_301_2354@.out.println("主线程组的名字:" + mainGroup.getName()); Sy@R_301_2354@.out.println("主线程组是否是后台线程组:" + mainGroup.isDaemon()); new MyThread("主线程组的线程").start(); ThreadGroup tg = new ThreadGroup("新线程组"); tg.setDaemon(true); Sy@R_301_2354@.out.println("tg线程组是否是后台线程组:" + tg.isDaemon()); MyThread tt = new MyThread(tg,"tg组的线程甲"); tt.start(); new MyThread(tg,"tg组的线程乙").start(); } }
2 operation
3 description
First, get the thread group to which the main thread belongs and access the relevant properties of the thread group, then create a new thread group and set the thread group as the background thread group.
For more Java related content, interested readers can view the special topics of this site: summary of java process and thread operation skills, tutorial on Java data structure and algorithm, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills
I hope this article will be helpful to you in Java programming.