Java : ThreadGroup - API使用例
ThreadGroup (Java SE 21 & JDK 21) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
ThreadGroup クラスを使うと、複数の Thread をグループとしてまとめることができます。
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());
// 結果
// ↓
//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
コンストラクタ
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
メソッド
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 ()
非推奨です。
final void destroy ()
非推奨です。
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();
// 結果
// ↓
//count = 4
//-- list --
//p1
//p2
//c1
//c2
int enumerate (Thread[] list, boolean recurse)
recurse = true は enumerate(Thread[] list) と同等です。
そちらのAPI使用例もご参照ください。
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();
// 結果
// ↓
//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());
}
// 結果
// ↓
//count = 5
//-- list --
//child-A
//child-child-A1
//child-child-A2
//child-B
//child-child-B1
int enumerate (ThreadGroup[] list, boolean recurse)
recurse = true は enumerate(ThreadGroup[] list) と同等です。
そちらのAPI使用例もご参照ください。
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());
}
// 結果
// ↓
//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 ()
このメソッドの使用例は、ThreadGroup(String name) にまとめて記載しました。
そちらのAPI使用例をご参照ください。
final ThreadGroup getParent ()
このメソッドの使用例は、ThreadGroup(ThreadGroup parent, String name) にまとめて記載しました。
そちらのAPI使用例をご参照ください。
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 --");
// 結果
// ↓
//-- start --
//task : start
//task : start
// InterruptedException!
// InterruptedException!
//-- join --
final boolean isDaemon ()
非推奨です。
boolean isDestroyed ()
非推奨です。
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 ()
非推奨です。
final void setDaemon (boolean daemon)
非推奨です。
final void setMaxPriority (int pri)
このメソッドの使用例は、getMaxPriority() にまとめて記載しました。
そちらのAPI使用例をご参照ください。
final void stop ()
非推奨です。
final void suspend ()
非推奨です。
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());
// 結果
// ↓
//-- start --
//uncaughtException
// t = thread-A
// e = java.lang.IllegalStateException: Error!
//-- join --
//activeCount : 0
関連記事
- API 使用例
- 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 (同期)
- Object (オブジェクト)
- Runnable
- Semaphore (セマフォ)
- Thread (スレッド)
- ThreadLocal
- TimeUnit