Skip to content
Advertisement

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?

Advertisement

Answer

Byteman

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:

JavaScript

See https://developer.jboss.org/docs/DOC-17213#how_do_i_inject_code_into_jvm_classes for further implementation details.


ByteBuddy

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.

JavaScript

Example code adapted from ByteBuddy github readme.

Advertisement