Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there thread group-local variables in Java?

I am looking for a class similar to ThreadLocal which would work on thread groups instead of threads.

If there is not such a class (in some open source library) how would you implement it? Some better idea than having thread groups in WeakHashMap?

I am implementing a debugging framework tunable in run-time with various parameters in global, per-thread and per-thread group contexts. As a very simple example, you can have a reporting statement:

debug.log( category, message);

and specify that the log entry with that specific category will be displayed only when called by a thread in group of threads servicing network requests.

like image 314
Viliam Avatar asked Jan 06 '10 23:01

Viliam


People also ask

Do threads share local variables Java?

Stack Memory and Threads Secondly, it stores local primitives and local object references on the stack. In addition, it's important to realize that every thread, including the main thread, has its own private stack. Therefore, other threads do not share our local variables, which is what makes them thread-safe.

Can threads have local variables?

The local variables of a function are unique to each thread that runs the function. However, the static and global variables are shared by all threads in the process. With thread local storage (TLS), you can provide unique data for each thread that the process can access using a global index.

What is ThreadLocal variable in Java?

ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread (e.g., a user ID or Transaction ID). For example, the class below generates unique identifiers local to each thread. A thread's id is assigned the first time it invokes ThreadId.

Do threads have their own variables?

Threads share all global variables; the memory space where global variables are stored is shared by all threads (though, as we will see, you have to be very careful about accessing a global variable from multiple threads). This includes class-static members!


2 Answers

ThreadGroup is rarely used, so there isn't platform support.

Using [Weak/Identity/Concurrent]HashMap<ThreadGroup,T> will just about work, if not very fast. You really want the map to be all weak, identity and concurrent but with the Java library you only get to choose one, currently.

To improve performance, note that Threads do not change ThreadGroup. Therefore, cache the value with a ThreadLocal (override initialValue). ThreadLocal has good performance (a couple dozen cycles per get).

like image 155
Tom Hawtin - tackline Avatar answered Oct 28 '22 11:10

Tom Hawtin - tackline


I would store a value holder in a thread local and initialize it to the same value holder for all threads of the same group.

 public class ThreadGroupLocal<T> extends ThreadLocal<ValueHolder> {
     private static class ValueHolder {
         public Object value;
     }
     // Weak & Concurrent would be even the better, but Java API wont offer that :(
     private static ConcurrentMap<ThreadGroup, ValueHolder> map = new ConcurrentHashMap<ThreadGroup, ValueHolder>;
     private static ValueHolder valueHolderForThread(Thread t) {
         map.putIfAbsent(t.getThreadGroup(), new ValueHolder());
         return map.get(t.getThreadGroup());
     }
     @Override 
     protected ValueHolder initialValue() {
         return valueHolderForThread(Thread.currentThread());
     }
     public T getValue() { (T) get().value; }
     public void setValue(T value) { get().value = value; }
 }

and then use

 ThreadGroupLocal<String> groupLocal = new ThreadGroupLocal<String>();
 groupLocal.setValue("foo");
 //...
 String foo = groupLocal.getValue();

That does (expect for the initialization) perform exactly like a thread local.

like image 32
akuhn Avatar answered Oct 28 '22 12:10

akuhn