Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread creation listener

Is it possible to write Thread creation listener in java? For example using aop?!

I mean something like this that if my application creates a thread I would like to register this object in my own table, container or something.

like image 598
Łukasz Rzeszotarski Avatar asked Sep 19 '12 08:09

Łukasz Rzeszotarski


3 Answers

I would create a thread that continously lists all running threads on the JVM.
Then each time it noticies that a new thread has appeared, it would notify in either way a class in your code.

Here are some links about how to list all threads currently running on the JVM :

  1. Get a List of all Threads currently running in Java

  2. Listing All Running Threads

============

A starting code :

ThreadCreationListener.java

public interface ThreadCreationListener {
    public void onThreadCreation(Thread newThread);
}

ThreadCreationMonitor.java

public class ThreadCreationMonitor extends Thread {
   private List<ThreadCreationListener> listeners;
   private boolean canGo;

   public ThreadCreationMonitor() {
      listeners = new Vector<ThreadCreationListener>();//Vector class is used because many threads may use a ThreadCreationMonitor instance.
      canGo = true;
      // Initialize the rest of the class here...
   }

   // Most important methods
   public void addListener(ThreadCreationListener tcl) {
        listeners.add(tcl);
   }

   public void removeListener(ThreadCreationListener tcl) {
        listeners.remove(tcl);
   }

   public void run() {
        List<Thread> runningThreads;
        List<Thread> lastRunningThreads = new ArrayList<>();

        while(canGo) {
            // Step 1 - List all running threads (see previous links)
            // runningThreads = ...

            // Step 2 - Check for new threads and notify all listeners if necessary
            if (runningThreads.removeAll(lastRunningThreads)==true) {
                for(Thread t : runningThreads) {
                    for(ThreadCreationListener tcl : listeners) {
                        lastRunningThreads.add(t);
                        tcl.onThreadCreation(t); //Notify listener
                    }
                }
            }
        }
   }

   public void shutdown() {
       canGo = false;
   }
}

MyThreadInfoConsumer.java

public class MyThreadInfoConsumer implements ThreadCreationListener {
    public void onThreadCreation(Thread newThread) {
        // Process here the notification...
    }
}

Main.java

public class Main {
    public static void main(String[] args) {
       ThreadCreationMonitor tcm = new ThreadCreationMonitor();
       tcm.start();

       MyThreadInfoConsumer myTIC = new MyThreadInfoConsumer();
       tcm.addListener(myTIC);

       // rest of your code...
       // Don't forget to call tcm.shutdown() when exiting your application !
    }
}
like image 170
Stephan Avatar answered Oct 18 '22 19:10

Stephan


I think this would be possible with AOP (aspectj for instance). But it is still required to create your own Thread and ThreadGroup/Executor types, unless you can recompile the JDK classes with the aspect compiler. Define the pointcut on your thread's start method if you want to register on thread launching or on the createThread of your pool if you want to register on the creation of the thread objects.


The following works only if you recompiled the JDK with the aspect compiler: All threads are started with Thread.start, so write a pointcut for that method then you can use advices to do what you'd like to. Of course this is not perfect since e.g. a cachedThreadPool executor might not start a new thread for each task, but maybe if you register a pointcut on Runnable.run and Callable.call rather than on Thread.start, that might be sufficient enough.

like image 33
zeller Avatar answered Oct 18 '22 19:10

zeller


Perhaps a ThreadGroup is what you need. All Threads are members of a ThreadGroup and when you start a new Thread it is added to the same group as its parent by default.

In theory its possible (but not recommended) to sub-class to be notified when a Thread is added or removed from the group.

It is likely that polling the threads of this groups, or polling all threads is a better solution.

like image 35
Peter Lawrey Avatar answered Oct 18 '22 19:10

Peter Lawrey