JavaRush /Java Blog /Random EN /ThreadGroup
Core
Level 35
Екатеринбург

ThreadGroup

Published in the Random EN group
Good afternoon and happy new year! Help me understand a little more about multithreading, namely ThreadGroup. I Googled, read, and understood the main essence: the purpose is security, integrated management through the organization of a group.
Threads are grouped into thread groups for manageability and security reasons. One thread group can belong to another group, forming a hierarchy with the main (system) group at the top level. Threads belonging to a group can be controlled at the same time - you have the right to interrupt the work of all threads in the group at once or set a single maximum execution priority value for them. Thread groups can also be used to define security domains. Threads within a group are usually endowed with the possibility of mutual influence, which also extends to threads of nested groups. When we say "influence," we mean that any method call can affect the behavior of the thread, say, change its priority, or cause an interruption.
(source: Tyts )
To prevent a single thread from starting to stop and interrupt all threads in a row, the concept of a group was introduced. A thread can only influence threads that are in the same group as it. A thread group is represented by the ThreadGroup class. This organization allows you to protect flows from unwanted external influences. A thread group can contain other groups, allowing all threads and groups to be organized into a hierarchical tree in which each ThreadGroup object, except the root one, has a parent.
(source: Tyts ) After reading various articles, I decided to write a small code in which I would not be able to interrupt the flows of another group from one thread of one group. And something didn’t work out: it allows me to interrupt((( My code: Why is it possible to interrupt? Here is the output: public class Test { public static ArrayList threads = new ArrayList (); public static ArrayList groups = new ArrayList (); public static void main(String[] args) throws InterruptedException { final ThreadGroup group1 = new ThreadGroup("GROUP 1"); final ThreadGroup group2 = new ThreadGroup("GROUP 2"); final ThreadGroup group3 = new ThreadGroup("GROUP 3"); groups.add(group1); groups.add(group2); groups.add(group3); for (int i = 0; i < groups.size(); i++) { for (int j = 1; j < 5; j++) { Thread thread = new Thread(groups.get(i), "THREAD №" + j) { @Override public void run() { while (!isInterrupted()) { try { Thread.sleep(500); } catch (InterruptedException e) { System.out.println(getName() + " : " + getThreadGroup().getName() + " прервана"); } } } }; threads.add(thread); thread.start(); } } Thread thread = new Thread(group1, "THREAD №5") { @Override public void run() { group2.interrupt(); while (true); } }; thread.start(); threads.add(thread); for (Thread t : threads) System.out.println(t); } } Thread[THREAD №1,5,GROUP 1] Thread[THREAD №2,5,GROUP 1] Thread[THREAD №3,5,GROUP 1] Thread[THREAD №4,5,GROUP 1] Thread[THREAD №1,5,GROUP 2] Thread[THREAD №2,5,GROUP 2] Thread[THREAD №3,5,GROUP 2] Thread[THREAD №4,5,GROUP 2] Thread[THREAD №1,5,GROUP 3] Thread[THREAD №2,5,GROUP 3] Thread[THREAD №3,5,GROUP 3] Thread[THREAD №4,5,GROUP 3] Thread[THREAD №5,5,GROUP 1] THREAD №1 : GROUP 2 прервана THREAD №2 : GROUP 2 прервана THREAD №4 : GROUP 2 прервана THREAD №3 : GROUP 2 прервана
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION