Debian Codegraph @

src/main/org/h2/util/DebuggingThreadLocal.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.util;
7
8 import java.util.HashMap;
9 import java.util.concurrent.ConcurrentHashMap;
10
11 /**
12 * Similar to ThreadLocal, except that it allows its data to be read from other
13 * threads - useful for debugging info.
14 *
15 * @param <T> the type
16 */
17 public class DebuggingThreadLocal<T> {
18
19 private final ConcurrentHashMap<Long, T> map = new ConcurrentHashMap<>();
20
21 public void set(T value) {
22 map.put(Thread.currentThread().getId(), value);
23 }
24
25 /**
26 * Remove the value for the current thread.
27 */
28 public void remove() {
29 map.remove(Thread.currentThread().getId());
30 }
31
32 public T get() {
33 return map.get(Thread.currentThread().getId());
34 }
35
36 /**
37 * Get a snapshot of the data of all threads.
38 *
39 * @return a HashMap containing a mapping from thread-id to value
40 */
41 public HashMap<Long, T> getSnapshotOfAllThreads() {
42 return new HashMap<>(map);
43 }
44
45 }