Java : InheritableThreadLocal - API使用例
InheritableThreadLocal (Java SE 21 & JDK 21) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
InheritableThreadLocal クラスは、親スレッドの値を引き継げる ThreadLocal です。
try (final var parent1 = Executors.newSingleThreadExecutor();
final var parent2 = Executors.newSingleThreadExecutor()) {
final var local = new InheritableThreadLocal<String>();
parent1.submit(() -> {
try {
System.out.println("parent 1 : set = abc");
local.set("abc");
TimeUnit.SECONDS.sleep(1);
System.out.println("parent 1 : get = " + local.get());
try (final var child = Executors.newSingleThreadExecutor()) {
child.submit(() -> {
System.out.println("child 1 : get = " + local.get());
});
}
} catch (InterruptedException e) {
System.out.println("InterruptedException!");
}
});
TimeUnit.MILLISECONDS.sleep(100);
parent2.submit(() -> {
try {
System.out.println("parent 2 : set = XYZ");
local.set("XYZ");
TimeUnit.SECONDS.sleep(1);
System.out.println("parent 2 : get = " + local.get());
try (final var child = Executors.newSingleThreadExecutor()) {
child.submit(() -> {
System.out.println("child 2 : get = " + local.get());
});
}
} catch (InterruptedException e) {
System.out.println("InterruptedException!");
}
});
}
// 結果
// ↓
//parent 1 : set = abc
//parent 2 : set = XYZ
//parent 1 : get = abc
//child 1 : get = abc
//parent 2 : get = XYZ
//child 2 : get = XYZ
コンストラクタ
InheritableThreadLocal ()
final var local = new InheritableThreadLocal<String>();
System.out.println(local.get()); // null
local.set("abc");
System.out.println(local.get()); // abc
final var local = new InheritableThreadLocal<Integer>();
System.out.println(local.get()); // null
local.set(123);
System.out.println(local.get()); // 123
メソッド
protected T childValue (T parentValue)
try (final var parent = Executors.newSingleThreadExecutor()) {
final var local = new InheritableThreadLocal<String>() {
@Override
protected String childValue(String parentValue) {
System.out.println("-- childValue --");
System.out.println(" parentValue = " + parentValue);
return parentValue.toUpperCase();
}
};
parent.submit(() -> {
local.set("abc");
System.out.println("parent : get = " + local.get());
try (final var child = Executors.newSingleThreadExecutor()) {
child.submit(() -> {
System.out.println("child : get = " + local.get());
});
}
});
}
// 結果
// ↓
//parent : get = abc
//-- childValue --
// parentValue = abc
//child : get = ABC
ThreadLocalで宣言されたメソッド
get, initialValue, remove, set, withInitial
「Java API 使用例 : ThreadLocal」をご参照ください。
関連記事
- 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 (スレッド)
- ThreadGroup
- ThreadLocal
- TimeUnit