0

I'm looking for verification on the following use of ThreadLocal.

I have a service, say ServiceA running on a set of processes, say processSetX in the system. Which processSetX will be on ServiceA isn't known until runtime and may vary. The processes in processSetX may run on different threads. ServiceA has to recognize all processes in processSetX the same way.

For this, I'm supposed to write an ID value, say, of type String, to thread local storage (TLS) of a new thread and read this value later on when needed. So, the ID of the first thread invoking ServiceA will be this ID for ServiceA to recognize them all. When this first thread starts another thread, it'll go onto this new thread's TLS and write this ID. From there on, every thread in this chain will pass this ID to the new one.

I'm looking to verify ThreadLocal is the way to work this.

I haven't used it before - I want to make sure.

TIA.

//==================

EDIT:

is there a way to get the calling thread's reference?

eg.:

a thread, say threadX is making a call to, say methodA(). is there a way for methodA() to know "who" is calling it? if so - methodA() is able to invoke a getter method of threadX to read a value from its thread-local storage.

TIA

//=================

EDIT-2:

Thread.currentThread() returns something like Thread[main,5,main]. this may collide across threads.

user4402742
  • 91
  • 1
  • 11
  • 1
    `Thread.currentThread()` will get you a reference to the `Thread` **instance** representing the currently executing thread, which is invoking your `methodA`. Note that if by _thread-local storage_, you mean values stored in a `ThreadLocal`, you don't need the current `Thread` instance. This is done behind the scenes for you. – Sotirios Delimanolis Mar 25 '15 at 19:58
  • `Thread#toString()` returns its name, its priority, and its group's name. What do you mean by _collide_? Don't use the `toString` value as a unique identifier for the `Thread`. Use the `Thread` itself (`==`) or its `getId` method. – Sotirios Delimanolis Mar 25 '15 at 22:01
  • @SotiriosDelimanolis read EDIT part - read it carefully. – user4402742 Mar 25 '15 at 22:25
  • The second one? `currentThread` returns a reference to the currently executing `Thread`. You've invoked `toString` on the referenced object and printed that out. That's the result you see `Thread[main,5,main]`. Yes, many threads may have the same name, `main`, with the same priority, `5`, and the same group name, `main`. That's why you should not use the `toString` result to identify your `Thread` instances. In that case, there will be no collision. Unless I misunderstand what you are referring to with _collide_, in which case please clarify. – Sotirios Delimanolis Mar 25 '15 at 22:29
  • I don't think you can do that unless the calling thread tells itself. – user3880721 Mar 26 '15 at 00:21
  • If @Roam comes in here, we'll have a trifecta. I'm trying to help you, but I need to understand you. Please don't work against me. – Sotirios Delimanolis Mar 26 '15 at 00:33

1 Answers1

0

I think at first, you just need a normal member variable.

For example:

// Thread
public class CalledThread extends Thread {

    public String myId;

    public void run() {
    ....

// Caller
    CalledThread t = new CalledThread();
    t.myId = "the thread ID";
    t.start();

However, you won't be able to access myId once you start calling your service classes, so for that you could use a ThreadLocal.

In CalledThread assign the myId to your ThreadLocal in run.

threadLocal.set(myId)
rghome
  • 8,529
  • 8
  • 43
  • 62