Nils
Nils

Reputation: 5987

How do you check which thread is executing code in Java?

I have a multi-threaded Java program with a bunch of rules around threading: For example, code in class A should only be called from the UI thread; 3 methods in class B must be called only from the network thread, etc.

Any suggestions on how to do assertions or other code checks that these rules are being followed? I'd like to do the equivalent of testing for "invariants" to prevent coding errors on thread usage.

Upvotes: 17

Views: 22718

Answers (6)

Israel Varea
Israel Varea

Reputation: 2630

You can also check thread name using logging e.g. log4j or logback, just set a pattern including %thread like this:

<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>
  <root level="INFO">
     <appender-ref ref="STDOUT" />
  </root>
</configuration>

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533870

You can try

assert Thread.currentThread() == expectedThread;

The problem with using the name is that any number of threads can use that name e.g. if you are particularly paranoid you might worry about code like this.

Thread.t = Thread.currentThread();
String name = t.getName();
t.setName("UI thread");
callUIThreadOnlyMethod();
t.setName(name);

Upvotes: 10

DwB
DwB

Reputation: 38328

Consider inverting the question. Consider prevention instead of enforcement.

If class Mangalor can only be run in a UI thread, limit the visibility of the Mangalor class to UI classes. If methods talk() and listen() of class CanOnString must only be run in a networking thread, limit the visibility of those methods to classes that you run in your networking thread.

Upvotes: 3

robert_x44
robert_x44

Reputation: 9324

In addition to adamfisk's excellent suggestion, there is also a convenience method for specifically testing whether the current thread is the EDT thread:

EventQueue.isDispatchThread()

Upvotes: 15

Arthur Neves
Arthur Neves

Reputation: 12138

I would do like this in my code in class A:

if(!"UI thread".equals(Thread.currentThread().getName())){
    throw new IllegalStateException("wrong thread running this class, thread name:"+Thread.currentThread().getName());
}

Upvotes: 1

adamfisk
adamfisk

Reputation: 578

Thread.currentThread().getName()

Upvotes: 22

Related Questions