线程创建监听器

Łuk*_*ski 7 java multithreading jvm classloader

是否可以在java中编写Thread创建监听器?例如使用aop?!

我的意思是这样的,如果我的应用程序创建一个线程,我想在我自己的表,容器或其他东西中注册这个对象.

Ste*_*han 5

我将创建一个线程来连续列出 JVM 上所有正在运行的线程。
然后,每次它注意到出现一个新线程时,它都会以任一方式通知代码中的一个类。

以下是一些有关如何列出 JVM 上当前运行的所有线程的链接:

  1. 获取当前在 Java 中运行的所有线程的列表

  2. 列出所有正在运行的线程

===========

起始代码:

ThreadCreationListener.java

public interface ThreadCreationListener {
    public void onThreadCreation(Thread newThread);
}
Run Code Online (Sandbox Code Playgroud)

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;
   }
}
Run Code Online (Sandbox Code Playgroud)

MyThreadInfoConsumer.java

public class MyThreadInfoConsumer implements ThreadCreationListener {
    public void onThreadCreation(Thread newThread) {
        // Process here the notification...
    }
}
Run Code Online (Sandbox Code Playgroud)

主程序.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 !
    }
}
Run Code Online (Sandbox Code Playgroud)


zel*_*ler 2

我认为这可以通过AOP(例如aspectj)实现。但仍然需要创建您自己的ThreadThreadGroup/Executor类型,除非您可以使用方面编译器重新编译 JDK 类。start如果您想在线程启动时注册,请在线程的方法上定义切入点;createThread如果您想在线程对象的创建时注册,请在池的方法上定义切入点。


仅当您使用方面编译器重新编译 JDK 时,以下内容才有效:所有线程均以 开头Thread.start,因此为该方法编写一个切入点,然后您可以使用建议来执行您想要的操作。当然,这并不完美,因为例如,cachedThreadPool 执行程序可能不会为每个任务启动一个新线程,但也许如果您在Runnable.run和 上注册切入点,Callable.call而不是 on Thread.start,这可能就足够了。

  • @用户454322。我明白了,我将这个问题解释为好像他想注册“线程启动”事件。我编辑了我的答案。 (2认同)