Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of java high concurrency midline group

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces what is the use of java high concurrency middle thread group. It is very detailed and has certain reference value. Friends who are interested must finish it!

Thread group

We can assign threads to a thread group, which can contain multiple threads and thread groups. Threads and thread groups form a parent-child relationship, which is a tree structure, as shown in the following figure:

Thread groups make it easy to manage threads, and thread groups provide some convenient ways for us to manage threads.

Create thread association thread group

When you create a thread, you can assign a thread group to the thread as follows:

Package com.itsoku.chat02; import java.util.concurrent.TimeUnit; / * description:

* time:2019/7/13 17:53

* author: Wechat official account: passer-by Java, specializing in java technology sharing (taking you to play with crawlers, distributed transactions, asynchronous messaging services, task scheduling, sub-database tables, big data, etc.), please follow! * / public class Demo1 {public static class R1 implements Runnable {@ Override public void run () {System.out.println ("threadName:" + Thread.currentThread () .getName ()); try {TimeUnit.SECONDS.sleep (3) } catch (InterruptedException e) {e.printStackTrace ();} public static void main (String [] args) throws InterruptedException {ThreadGroup threadGroup = new ThreadGroup ("thread-group-1"); Thread T1 = new Thread (threadGroup, new R1 (), "T1") Thread T2 = new Thread (threadGroup, new R1 (), "T2"); t1.start (); t2.start (); TimeUnit.SECONDS.sleep (1); System.out.println ("active threads:" + threadGroup.activeCount ()); System.out.println ("active thread group:" + threadGroup.activeGroupCount ()) System.out.println ("thread group name:" + threadGroup.getName ());}}

Output result:

ThreadName:t1

ThreadName:t2

Number of active threads: 2

Active thread group: 0

Thread group name: thread-group-1

The activeCount () method returns the number of all active threads in the thread group, including the threads of all descendant nodes below, which can only be an estimate because the threads in the thread group are dynamic.

Specify a parent thread group for a thread group

When you create a thread group, you can assign it a parent thread group or not. If you do not specify a parent thread group, the parent thread group is the thread group of the current thread. Java api has two common construction methods to create thread groups:

Public ThreadGroup (String name) public ThreadGroup (ThreadGroup parent, String name)

The first constructor does not specify a parent thread group. Take a look at the internal implementation:

Public ThreadGroup (String name) {this (Thread.currentThread (). GetThreadGroup (), name);}

The system automatically gets the thread group of the current thread as the default parent thread group.

The sample code in the previous paragraph:

Package com.itsoku.chat02; import java.util.concurrent.TimeUnit; / * description:

* time:2019/7/13 17:53

* author: Wechat official account: passer-by Java, specializing in java technology sharing (taking you to play with crawlers, distributed transactions, asynchronous messaging services, task scheduling, sub-database tables, big data, etc.), please follow! * / public class Demo2 {public static class R1 implements Runnable {@ Override public void run () {Thread thread = Thread.currentThread (); System.out.println ("thread group: + thread.getThreadGroup () .thread () +", thread name: "+ thread.getName ()) Try {TimeUnit.SECONDS.sleep (3);} catch (InterruptedException e) {e.printStackTrace ();} public static void main (String [] args) throws InterruptedException {ThreadGroup threadGroup1 = new ThreadGroup ("thread-group-1") Thread T1 = new Thread (threadGroup1, new R1 (), "T1"); Thread T2 = new Thread (threadGroup1, new R1 (), "T2"); t1.start (); t2.start (); TimeUnit.SECONDS.sleep (1); System.out.println ("number of threadGroup1 active threads:" + threadGroup1.activeCount ()) System.out.println ("threadGroup1 active thread group:" + threadGroup1.activeGroupCount ()); System.out.println ("threadGroup1 thread group name:" + threadGroup1.getName ()); System.out.println ("threadGroup1 parent thread group name:" + threadGroup1.getParent (). GetName ()); System.out.println ("-") ThreadGroup threadGroup2 = new ThreadGroup (threadGroup1, "thread-group-2"); Thread T3 = new Thread (threadGroup2, new R1 (), "T3"); Thread T4 = new Thread (threadGroup2, new R1 (), "T4"); t3.start (); t4.start (); TimeUnit.SECONDS.sleep (1) System.out.println ("number of threadGroup2 active threads:" + threadGroup2.activeCount ()); System.out.println ("threadGroup2 active thread group:" + threadGroup2.activeGroupCount ()); System.out.println ("threadGroup2 thread group name:" + threadGroup2.getName ()); System.out.println ("threadGroup2 parent thread group name:" + threadGroup2.getParent () .thread () System.out.println ("- -"); System.out.println ("number of threadGroup1 active threads:" + threadGroup1.activeCount ()); System.out.println ("threadGroup1 active thread group:" + threadGroup1.activeGroupCount ()); System.out.println ("- -") ThreadGroup1.list ();}}

Output result:

Thread group: thread-group-1, thread name: T1

Thread group: thread-group-1, thread name: T2

Number of threadGroup1 active threads: 2

ThreadGroup1 active thread group: 0

ThreadGroup1 thread group name: thread-group-1

ThreadGroup1 parent thread group name: main

--

Thread group: thread-group-2, thread name: T4

Thread group: thread-group-2, thread name: T3

Number of threadGroup2 active threads: 2

ThreadGroup2 active thread group: 0

ThreadGroup2 thread group name: thread-group-2

ThreadGroup2 parent thread group name: thread-group-1

--

Number of threadGroup1 active threads: 4

ThreadGroup1 active thread group: 1

--

Java.lang.ThreadGroup [name=thread-group-1,maxpri=10]

Thread [t1mem5threadlygroupway1]

Thread [t2pyrrine 5 threadmuri groupmuri 1]

Java.lang.ThreadGroup [name=thread-group-2,maxpri=10]

Thread [t3pyrrine 5 threadmuri groupmuri 2]

Thread [t4pyrrine 5 threadwood groupmuri 2]

Code interpretation:

1.threadGroup1 does not specify a parent thread group, and the system acquires the thread group of the main thread as the parent thread group of threadGroup1. The output result is: main

2.threadGroup1 is the parent thread group of threadGroup2

The number of 3.threadGroup1 active threads is 4, including T1 and T2 in threadGroup1 thread group, and T3 and T4 in sub-thread group threadGroup2.

4. The list () method of the thread group, which outputs the information of all descendant nodes in the thread group to the console for debugging and using

Root thread group

Get the root thread group

Package com.itsoku.chat02; / * description:

* time:2019/7/13 17:53

* author: Wechat official account: passer-by Java, specializing in java technology sharing (taking you to play with crawlers, distributed transactions, asynchronous messaging services, task scheduling, sub-database tables, big data, etc.), please follow! * / public class Demo3 {public static void main (String [] args) {System.out.println (Thread.currentThread ()); System.out.println (Thread.currentThread (). GetThreadGroup ()); System.out.println (Thread.currentThread (). GetThreadGroup (). GetParent ()); System.out.println (Thread.currentThread (). GetThreadGroup (). GetParent (). GetParent ()) }}

Run the above code and output:

Thread [main,5,main]

Java.lang.ThreadGroup [name=main,maxpri=10]

Java.lang.ThreadGroup [name=system,maxpri=10]

Null

As can be seen from the above code:

1. The thread group of the main thread is main

two。 The root thread group is system

Take a look at the ThreadGroup source code:

Private ThreadGroup () {/ / called from C code this.name = "system"; this.maxPriority = Thread.MAX_PRIORITY; this.parent = null;}

It is found that the default constructor of ThreadGroup is private, which is called by c, and the system thread group is created.

Batch stop thread

Calling thread group interrupt () sets the interrupt flag of all descendant threads under the thread group tree to true, which can be used to interrupt threads in batches.

Sample code:

Package com.itsoku.chat02; import java.util.concurrent.TimeUnit; / * description:

* time:2019/7/13 17:53

* author: Wechat official account: passer-by Java, specializing in java technology sharing (taking you to play with crawlers, distributed transactions, asynchronous messaging services, task scheduling, sub-database tables, big data, etc.), please follow! * / public class Demo4 {public static class R1 implements Runnable {@ Override public void run () {Thread thread = Thread.currentThread (); System.out.println ("thread group: + thread.getThreadGroup () .thread () +", thread name: "+ thread.getName ()) While (! thread.isInterrupted ()) {;} System.out.println ("Thread:" + thread.getName () + "stopped!") ;} public static void main (String [] args) throws InterruptedException {ThreadGroup threadGroup1 = new ThreadGroup ("thread-group-1"); Thread T1 = new Thread (threadGroup1, new R1 (), "T1"); Thread T2 = new Thread (threadGroup1, new R1 (), "T2"); t1.start (); t2.start () ThreadGroup threadGroup2 = new ThreadGroup (threadGroup1, "thread-group-2"); Thread T3 = new Thread (threadGroup2, new R1 (), "T3"); Thread T4 = new Thread (threadGroup2, new R1 (), "T4"); t3.start (); t4.start (); TimeUnit.SECONDS.sleep (1) System.out.println ("- threadGroup1 information -"); threadGroup1.list (); System.out.println ("- -"); System.out.println ("stop thread group: all descendant threads in" + threadGroup1.getName () + ") ThreadGroup1.interrupt (); TimeUnit.SECONDS.sleep (2); System.out.println ("output information -" after-threadGroup1 stops); threadGroup1.list ();}}

Output:

Thread group: thread-group-1, thread name: T1

Thread group: thread-group-1, thread name: T2

Thread group: thread-group-2, thread name: T3

Thread group: thread-group-2, thread name: T4

-threadGroup1 information-

Java.lang.ThreadGroup [name=thread-group-1,maxpri=10]

Thread [t1mem5threadlygroupway1]

Thread [t2pyrrine 5 threadmuri groupmuri 1]

Java.lang.ThreadGroup [name=thread-group-2,maxpri=10]

Thread [t3pyrrine 5 threadmuri groupmuri 2]

Thread [t4pyrrine 5 threadwood groupmuri 2]

--

Stop thread group: all descendant threads in thread-group-1

Thread: T4 stopped!

Thread: T2 stopped!

Thread: T1 stopped!

Thread: T3 stopped!

-after the threadGroup1 stops, the output information-

Java.lang.ThreadGroup [name=thread-group-1,maxpri=10]

Java.lang.ThreadGroup [name=thread-group-2,maxpri=10]

After stopping the thread, you can see through the list () method that the output does not contain the finished thread.

These are all the contents of this article entitled "what is the use of the java high concurrency midline group?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report