Explanation
1. enumerate() can be used to copy Thread and ThreadGroup.
2. ThreadGroup can add several Threads and several sub-ThreadGroups. This method can be used to easily copy.
Example
public static void main(String[] args) throws InterruptedException { ThreadGroup myGroup = new ThreadGroup("MyGroup"); Thread thread = new Thread(myGroup,()->{ while (true){ try{ TimeUnit.SECONDS.sleep(1); }catch (InterruptedException e){ e.printStackTrace(); } } },"MyThread"); thread.start(); TimeUnit.MILLISECONDS.sleep(1); ThreadGroup mainGroup = currentThread().getThreadGroup(); Thread[] list = new Thread[mainGroup.activeCount()]; int recurseSize = mainGroup.enumerate(list); System.out.println(recurseSize); recurseSize = mainGroup.enumerate(list,false); System.out.println(recurseSize); }
The above is the detailed content of How to copy using enumerate() method in Java?. For more information, please follow other related articles on the PHP Chinese website!