Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add attributes to Current Java Thread

How to I could set 'attributes' to current Thread in Java, I want to set key-values and get the value in another place, but in the same Thread. like to this http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/MDC.html

like image 439
jrey Avatar asked Apr 10 '11 01:04

jrey


People also ask

How do you get reference to the current thread in Java?

In the run() method, we use the currentThread(). getName() method to get the name of the current thread that has invoked the run() method. We use the currentThread(). getId() method to get the id of the current thread that has invoked the run() method.

What are thread attributes?

Thread attributes provide a mechanism for fine-tuning the behavior of individual threads. You may create and customize a thread attribute object to specify other values for the attributes. To specify customized thread attributes, you must follow these steps; Create a pthread_attr_t object.


1 Answers

I do not think you can add attributes to any given thread in Java, but you could use a ThreadLocal instance to store any special information per thread.

http://download.oracle.com/javase/6/docs/api/java/lang/ThreadLocal.html

like image 104
Edwin Dalorzo Avatar answered Oct 03 '22 01:10

Edwin Dalorzo