In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what are the functions and methods of java ThreadGroup". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the functions and methods of java ThreadGroup".
Catalogue
The function and method of java ThreadGroup
Field
Private construction method
Common construction method
Public method
A special method.
Java thread group (ThreadGroup class)
The function and method of java ThreadGroup
ThreadGroup thread group, java's description of this class is
"Thread groups represent a set of threads. In addition, thread groups can include other thread groups. Thread groups form a tree in which each thread group except the initial thread group has a parent thread group.
Allow threads to access information about their thread groups, but not about their thread group's parent thread group or any other thread group. "
ThreadGroup is not a label container, because eventually you'll find that this guy doesn't have public's add,remove method. So how do you put a thread in a thread group?
The answer is in new Thread (parameter), passing ThreadGroup as a parameter.
Thread group of Fieldprivate final ThreadGroup parent;// thread group, final table name thread group cannot change String name; / / name thread group elements such as the maximum priority of thread group. The specific implementation is that when the thread or thread group itself sets the priority, it always takes the priority of its own parent thread group and the minimum value boolean destroyed of the priority to be set. / / determine whether the boolean daemon;// is destroyed. When the last thread of the daemon thread group stops or the last thread group is destroyed, the thread group will be automatically destroyed. Int nUnstartedThreads = 0 int nthreads;// the number of threads in this thread group Thread threads []; / Thread array, holds the thread reference int ngroups;// the number of thread groups in this thread group ThreadGroup groups []; / / thread group array, holds the thread group reference private constructor
Create an empty thread group that is not in any thread group.
This method is used to create a system thread group.
The private ThreadGroup () public constructor / / creates a new thread group. The parent thread group of this new group is the specified thread group parent. The name of the thread group is that name calls checkAccess () on parent to determine whether the currently running thread has permission to modify the thread group (such as setting setDaemon). It is possible to throw a SecurityException exception public ThreadGroup (ThreadGroup parent, String name) / / to construct a new thread group. The parent thread group of this new group is the thread group of the currently running thread. Is to call the above method public ThreadGroup (String name) {this (Thread.currentThread (). GetThreadGroup (), name);} public method public final String getName () / return thread group name / / return parent thread group parent call checkAccess () to determine whether the currently running thread has permission to modify this thread group. / / it is possible to throw a SecurityException exception public final ThreadGroup getParent () public final int getMaxPriority () / return thread group priority / / test whether this thread group is a daemon thread group. When the last thread of the daemon thread group stops or the last thread group is destroyed, the thread group is automatically destroyed. Public final boolean isDaemon () public synchronized boolean isDestroyed () / tests whether the thread group has been destroyed. Public final void setDaemon (boolean daemon) / / sets the thread group to the daemon thread group, which checks whether the current thread has permission to modify the thread group / / sets the priority of the current thread group and the child thread group, whichever is lower than the priority of the parent thread group of the pri and the current thread group. / / this limits the maximum priority of Thread / / the specific implementation is that when a thread or thread group sets its own priority, it always takes the priority of its parent thread group and the minimum value of the priority to be set / / will check whether the current thread has permission to modify thread group public final void setMaxPriority (int pri) / / test Whether the current thread group is the parent of the g thread group or the parameter public final boolean parentOf (ThreadGroup g) / / check whether the current thread has the permission to modify the thread group. For example, if the thread group itself calls some of its own methods in the current thread, it will check public final void checkAccess () / / and return an estimate of the number of active threads in this thread group and its child thread groups. Recursively iterate through all subgroups in this thread group. If the current thread group is already destroyed, return 0 public int activeCount () / / putting the active thread in the thread group into list [] will automatically expand the array. If {@ code recurse} is {@ code true}, this method will recursively enumerate all the subgroups of this thread group. And reference each active thread in these subgroups / / Note the way the array is declared public int enumerate (Thread list [], boolean recurse) / / is similar to the method above, except that the following ThreadGrouppublic int enumerate (ThreadGroup list []) public int enumerate (ThreadGroup list [], boolean recurse) / / returns an estimate of the number of active groups in this thread group and its subgroups. Recursively iterate through all subgroups in this thread group. Public int activeGroupCount () / / interrupt all threads in this thread group. A special method public void uncaughtException (Thread t, Throwable e) that includes threads in the subthread group public final void interrupt ()
This method simply causes the t thread to throw an exception of Throwable e. This e exception is also defined by you. If the previous setting is
Thread.setDefaultUncaughtExceptionHandler ((t, e)-> {System.out.println (default + t.getName ()); System.out.println (default + e);})
Then, the Throwable customized above will be captured by this, and if not set, the annotation error stream will be printed. I am confused about the meaning of existence of this method.
Thread.setDefaultUncaughtExceptionHandler ((t, e)-> {System.out.println ("default" + t.getName (); System.out.println ("default" + e);}); ThreadGroup threadGroup = new ThreadGroup ("father"); Thread two = new Thread (threadGroup, "two"); threadGroup.uncaughtException (two,new IllegalAccessException ("ssss"))
Running result
Java thread group (ThreadGroup class)
A thread group represents a collection of threads. Thread groups can also contain other thread groups. After the thread is executed, it will be reclaimed.
Private static void methodA () {MyThread my = new MyThread (); / / create thread class object Thread T1 = new Thread (my, "Thread 1"); Thread T2 = new Thread (my, "Thread 2"); / / public final ThreadGroup getThreadGroup () returns the thread group ThreadGroup tg1 = t1.getThreadGroup () to which the thread belongs; ThreadGroup tg2 = thread () / / public final String getName (): returns the name of the thread group System.out.println (tg1.getName ()); / / main System.out.println (tg2.getName ()); / / main / / all threads its default thread group name: main (main thread), which is the System.out.println contained in the thread group (Thread.currentThread (). GetThreadGroup (). GetName ()) / / main} private static void methodB () {/ / public ThreadGroup (String name) construct a new thread group ThreadGroup tg = new ThreadGroup ("new thread group"); MyThread my = new MyThread (); / / Thread (ThreadGroup group, Runnable target, String name) Thread T1 = new Thread (tg, my, "Thread 1"); Thread T2 = new Thread (tg, my, "Thread 2") / / get the thread group name System.out.println (t1.getThreadGroup () .thread ()) directly; System.out.println (t2.getThreadGroup () .getName ()) Thank you for your reading. The above is the content of "what are the functions and methods of java ThreadGroup". After the study of this article, I believe you have a deeper understanding of the role and methods of java ThreadGroup, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.