Good afternoon and happy new year! Help me understand a little about multithreading, namely with ThreadGroup. Googled, read, understood the main point: the purpose is security, integrated management through the organization of the grouping.
Threads are combined into thread groups for reasons of improved manageability and security. 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 once - you can 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 is extended to the threads of nested groups. By "influence" we mean that calling any method can affect the characteristics of the thread's Behavior, such as changing its priority or interrupting it.(source: Tyts )
In order to prevent a single thread from starting to stop and interrupt all threads in a row, the concept of a group has been introduced. A thread can only affect threads that are in the same group as it. A thread group is represented by the ThreadGroup class. Such an organization allows you to protect flows from unwanted external influences. A thread group can contain other groups, which allows all threads and groups to be organized into a hierarchical tree in which every ThreadGroup except the root 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 threads of another group from one thread of one group. And something didn’t work out: it gives 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 прервана
GO TO FULL VERSION