Check which thread is calling a static method

In my engine, I have a static method that can be called from two different threads.

I would like for a way to know which thread is calling the method. I want to change the method slightly depending on the thread, but would like to still just use 1 method.

Any thoughts would be appreciated!

For example:


// Thread 1
a();

// Thread 2
a();

static void a() {
   if ( getThread() == THREAD_1 ) {
      // Do stuff
   } else {
      // Do other stuff
   }

   // Do even more stuff
}

This seems to have what you’re looking for.

Ah perfect. Thanks! :slight_smile:

Wait… why don’t you just call the method with a differing parameter?

This can quickly become cumbersome because you would have to drag that parameter through all method calls of your callstack, starting with the Thread’s initial Runnable to finally the method you want to check the thread in.
Thread-local memory is also one way to solve this (via Java’s java.lang.ThreadLocal).

@KaiHH: True. From the wording I assumed there were just exactly two seperate uses of it but I see now that it was indeed not necessarily implied.