Skip to content

Is it possible to hook thread creation?

I am making a plugin system and i need to see when a plugin calls Thread.start() Is there a way similar to Runtime.getRuntime().addShutdownHook but for hooking when a thread starts?




You can use Byteman to inject your own code into the thread.start() method.

In fact, the first example to using Byteman with JVM classes on their website is one showing how to print to the console when a thread has started.

Byteman script example from their tutorial:

RULE trace thread start

CLASS java.lang.Thread

METHOD start()

IF true

DO traceln("*** start for thread: "+ $0.getName())


See for further implementation details.


If Byteman isn’t your thing, there’s another library called ByteBuddy which can be used to create a Java Agent that intercepts a method in the Thread class.

public class ThreadMonitor {
  public static Object intercept(@Origin Method method, 
                                 @SuperCall Callable<?> callable) {
    System.out.println("A thread start method called");
      return; //Calling the original start method.

public class ThreadMonitorAgent {
  public static void premain(String arguments, 
                             Instrumentation instrumentation) {
    new AgentBuilder.Default()
      .transform((builder, type, classLoader, module) -> 

Example code adapted from ByteBuddy github readme.

3 People found this is helpful