Search code examples
javamultithreadinginstrumentationjava-bytecode-asmjavaagents

Is load time Bytecode Instrumentation done in parallel in Java?


So, I made my own Java agent which has a ClassFileTransformer in it which implements the transform method. What I observed is that this method is called on different threads. I checked it with Thread.currentThread() These are the different threads:

Thread[background-preinit,5,main]
Thread[main,5,main]
Thread[cluster-ClusterId{value='...', description='null'}-localhost:27017,5,main]

Solution

  • A ClassFileTransformer is applied in the thread that loads a class for the first time. In case of a retransformation, all loaded classes are instrumented in the thread that invokes Instrumentation.retransformClasses.

    Therefore: yes, retransformation happens in multiple threads.