Home - Search - Site Map - Site Graph | Contact
( Last Modified: 2009 February 11th )

Find all anonymous threads in java

This example uses java version: jdk1.6.0_01.

The problem with unnamed threads

If you have a large project it's difficult to find all threads that are created without a name.

You may want to find and name unnamed threads from you project so you don't get:

thread-1
thread-2
thread-3

names in traces and stack dumps.

The difficuly of finding all unnamed Threads

A search would have to cover all the different ways of creating threads,
including inner/anonymous class delcarations and extensions of the Thread class.

Find all unnamed threads by removing Thread constructors

Instead of using a text search, it's possible to remove the constructors from the Thread class that do not include a name from java library.

Here is a copy of the thread class (java 1.6) that already has this done:

Thread_onlyInitWithName.class

You can download this, rename it to Thread.class, and replace the current .class file in jre/lib/rt.jar under the java install directory.

Now it will be impossible to create an unnamed/anonymous threads in you java project.

What construtors were removed

These are the constructors that were removed from the Thread.class above using a byte code editor.

init<> ()V
init<> (Ljava/lang/Runnable;)V
init<> (Ljava/lang/ThreadGroup;Ljava/lang/Runnable;)V

This may be useful to know if you want to remove theses constructors for another java version.

( Page Created: 2009 February 11th )