Java : ThreadGroup with Examples
ThreadGroup (Java SE 21 & JDK 21) with Examples.
You will find code examples on most ThreadGroup methods.
Summary
final var group = new ThreadGroup("group-A");
System.out.println("group : " + group);
final Runnable task = () -> {
try {
final var current = Thread.currentThread();
System.out.println(" run : " + current);
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
System.out.println("InterruptedException!");
}
};
final var threadA = new Thread(group, task, "name-A");
final var threadB = new Thread(group, task, "name-B");
System.out.println("-- start --");
threadA.start();
threadB.start();
System.out.println("activeCount = " + group.activeCount());
threadA.join();
threadB.join();
System.out.println("-- join --");
System.out.println("activeCount = " + group.activeCount());
// Result
// ↓
//group : java.lang.ThreadGroup[name=group-A,maxpri=10]
//-- start --
// run : Thread[#32,name-A,5,group-A]
// run : Thread[#33,name-B,5,group-A]
//activeCount = 2
//-- join --
//activeCount = 0
Constructors
ThreadGroup (String name)
final var group1 = new ThreadGroup("abc");
System.out.println(group1.getName()); // abc
final var group2 = new ThreadGroup("XYZ");
System.out.println(group2.getName()); // XYZ
ThreadGroup (ThreadGroup parent, String name)
final var parent = new ThreadGroup("parent");
System.out.println(parent.getName()); // parent
final var childA = new ThreadGroup(parent, "child-A");
final var childB = new ThreadGroup(parent, "child-B");
System.out.println(childA.getName()); // child-A
System.out.println(childB.getName()); // child-B
System.out.println(parent.getParent().getName()); // main
System.out.println(childA.getParent().getName()); // parent
System.out.println(childB.getParent().getName()); // parent
Methods
int activeCount ()
final Runnable task = () -> {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
System.out.println("InterruptedException!");
}
};
final var parent = new ThreadGroup("parent");
final var p1 = new Thread(parent, task);
final var p2 = new Thread(parent, task);
final var child = new ThreadGroup(parent, "child");
final var c1 = new Thread(child, task);
final var c2 = new Thread(child, task);
System.out.println(parent.activeCount()); // 0
System.out.println(child.activeCount()); // 0
p1.start();
p2.start();
c1.start();
c2.start();
System.out.println(parent.activeCount()); // 4
System.out.println(child.activeCount()); // 2
p1.join();
p2.join();
c1.join();
c2.join();
System.out.println(parent.activeCount()); // 0
System.out.println(child.activeCount()); // 0
int activeGroupCount ()
final var parent = new ThreadGroup("parent");
final var childA = new ThreadGroup(parent, "child-A");
final var childB = new ThreadGroup(parent, "child-B");
final var childChildA1 = new ThreadGroup(childA, "child-child-A1");
final var childChildA2 = new ThreadGroup(childA, "child-child-A2");
final var childChildB1 = new ThreadGroup(childB, "child-child-B1");
System.out.println(parent.activeGroupCount()); // 5
System.out.println(childA.activeGroupCount()); // 2
System.out.println(childB.activeGroupCount()); // 1
System.out.println(childChildA1.activeGroupCount()); // 0
System.out.println(childChildA2.activeGroupCount()); // 0
System.out.println(childChildB1.activeGroupCount()); // 0
final void checkAccess ()
Deprecated.
final void destroy ()
Deprecated.
int enumerate (Thread[] list)
final Runnable task = () -> {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
System.out.println("InterruptedException!");
}
};
final var parent = new ThreadGroup("parent");
final var p1 = new Thread(parent, task, "p1");
final var p2 = new Thread(parent, task, "p2");
final var child = new ThreadGroup(parent, "child");
final var c1 = new Thread(child, task, "c1");
final var c2 = new Thread(child, task, "c2");
p1.start();
p2.start();
c1.start();
c2.start();
final var list = new Thread[parent.activeCount()];
final var count = parent.enumerate(list);
System.out.println("count = " + count);
System.out.println("-- list --");
for (int i = 0; i < count; i++) {
System.out.println(list[i].getName());
}
p1.join();
p2.join();
c1.join();
c2.join();
// Result
// ↓
//count = 4
//-- list --
//p1
//p2
//c1
//c2
int enumerate (Thread[] list, boolean recurse)
enumerate (list, true) is equivalent to enumerate(Thread[] list).
final Runnable task = () -> {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
System.out.println("InterruptedException!");
}
};
final var parent = new ThreadGroup("parent");
final var p1 = new Thread(parent, task, "p1");
final var p2 = new Thread(parent, task, "p2");
final var child = new ThreadGroup(parent, "child");
final var c1 = new Thread(child, task, "c1");
final var c2 = new Thread(child, task, "c2");
p1.start();
p2.start();
c1.start();
c2.start();
final var list = new Thread[parent.activeCount()];
final var count = parent.enumerate(list, false);
System.out.println("count = " + count);
System.out.println("-- list --");
for (int i = 0; i < count; i++) {
System.out.println(list[i].getName());
}
p1.join();
p2.join();
c1.join();
c2.join();
// Result
// ↓
//count = 2
//-- list --
//p1
//p2
int enumerate (ThreadGroup[] list)
final var parent = new ThreadGroup("parent");
final var childA = new ThreadGroup(parent, "child-A");
final var childB = new ThreadGroup(parent, "child-B");
new ThreadGroup(childA, "child-child-A1");
new ThreadGroup(childA, "child-child-A2");
new ThreadGroup(childB, "child-child-B1");
final var list = new ThreadGroup[parent.activeGroupCount()];
final var count = parent.enumerate(list);
System.out.println("count = " + count);
System.out.println("-- list --");
for (int i = 0; i < count; i++) {
System.out.println(list[i].getName());
}
// Result
// ↓
//count = 5
//-- list --
//child-A
//child-child-A1
//child-child-A2
//child-B
//child-child-B1
int enumerate (ThreadGroup[] list, boolean recurse)
enumerate (list, true) is equivalent to enumerate(ThreadGroup[] list).
final var parent = new ThreadGroup("parent");
final var childA = new ThreadGroup(parent, "child-A");
final var childB = new ThreadGroup(parent, "child-B");
new ThreadGroup(childA, "child-child-A1");
new ThreadGroup(childA, "child-child-A2");
new ThreadGroup(childB, "child-child-B1");
final var list = new ThreadGroup[parent.activeGroupCount()];
final var count = parent.enumerate(list, false);
System.out.println("count = " + count);
System.out.println("-- list --");
for (int i = 0; i < count; i++) {
System.out.println(list[i].getName());
}
// Result
// ↓
//count = 2
//-- list --
//child-A
//child-B
final int getMaxPriority ()
final var group = new ThreadGroup("group");
System.out.println(group.getMaxPriority()); // 10
final var thread = new Thread(group, () -> {
}, "thread");
System.out.println(thread.getPriority()); // 5
final var group = new ThreadGroup("group");
group.setMaxPriority(Thread.MIN_PRIORITY);
System.out.println(group.getMaxPriority()); // 1
final var thread = new Thread(group, () -> {
}, "thread");
System.out.println(thread.getPriority()); // 1
final String getName ()
Please see ThreadGroup(String name).
final ThreadGroup getParent ()
Please see ThreadGroup(ThreadGroup parent, String name).
final void interrupt ()
final Runnable task = () -> {
try {
System.out.println("task : start");
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
System.out.println(" InterruptedException!");
}
};
final var group = new ThreadGroup("group");
final var t1 = new Thread(group, task, "t1");
final var t2 = new Thread(group, task, "t2");
System.out.println("-- start --");
t1.start();
TimeUnit.MILLISECONDS.sleep(100);
t2.start();
TimeUnit.MILLISECONDS.sleep(100);
group.interrupt();
t1.join();
t2.join();
System.out.println("-- join --");
// Result
// ↓
//-- start --
//task : start
//task : start
// InterruptedException!
// InterruptedException!
//-- join --
final boolean isDaemon ()
Deprecated.
boolean isDestroyed ()
Deprecated.
void list ()
final var group1 = new ThreadGroup("abc");
group1.list(); // java.lang.ThreadGroup[name=abc,maxpri=10]
final var group2 = new ThreadGroup("XYZ");
group2.setMaxPriority(Thread.MIN_PRIORITY);
group2.list(); // java.lang.ThreadGroup[name=XYZ,maxpri=1]
final boolean parentOf (ThreadGroup g)
final var parent = new ThreadGroup("parent");
final var child = new ThreadGroup(parent, "child");
final var childChild = new ThreadGroup(child, "child-child");
System.out.println(parent.parentOf(parent)); // true
System.out.println(parent.parentOf(child)); // true
System.out.println(parent.parentOf(childChild)); // true
System.out.println(child.parentOf(parent)); // false
System.out.println(child.parentOf(child)); // true
System.out.println(child.parentOf(childChild)); // true
System.out.println(childChild.parentOf(parent)); // false
System.out.println(childChild.parentOf(child)); // false
System.out.println(childChild.parentOf(childChild)); // true
final void resume ()
Deprecated.
final void setDaemon (boolean daemon)
Deprecated.
final void setMaxPriority (int pri)
Please see getMaxPriority().
final void stop ()
Deprecated.
final void suspend ()
Deprecated.
String toString ()
final var group1 = new ThreadGroup("abc");
final var str1 = group1.toString();
System.out.println(str1); // java.lang.ThreadGroup[name=abc,maxpri=10]
final var group2 = new ThreadGroup("XYZ");
group2.setMaxPriority(Thread.MIN_PRIORITY);
final var str2 = group2.toString();
System.out.println(str2); // java.lang.ThreadGroup[name=XYZ,maxpri=1]
void uncaughtException (Thread t, Throwable e)
final var group = new ThreadGroup("group") {
@Override
public void uncaughtException(Thread t, Throwable e) {
super.uncaughtException(t, e);
System.out.println("uncaughtException");
System.out.println(" t = " + t.getName());
System.out.println(" e = " + e);
}
};
final var threadA = new Thread(group, () -> {
throw new IllegalStateException("Error!");
}, "thread-A");
System.out.println("-- start --");
threadA.start();
threadA.join();
System.out.println("-- join --");
System.out.println("activeCount : " + group.activeCount());
// Result
// ↓
//-- start --
//uncaughtException
// t = thread-A
// e = java.lang.IllegalStateException: Error!
//-- join --
//activeCount : 0
Related posts
- API Examples
- BlockingQueue
- Callable
- CancellationException
- ConcurrentHashMap.KeySetView
- ConcurrentLinkedDeque
- ConcurrentLinkedQueue
- ConcurrentMap
- ConcurrentModificationException
- ConcurrentSkipListSet
- Condition
- CopyOnWriteArrayList
- CopyOnWriteArraySet
- CountDownLatch
- CyclicBarrier
- Exchanger
- Executor
- ExecutorService
- Executors
- Future
- Future.State
- FutureTask
- InterruptedException
- Lock
- Runnable
- Semaphore
- Thread
- ThreadLocal
- TimeUnit