有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java线程创建侦听器

可以用java编写线程创建侦听器吗?例如使用aop

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


共 (3) 个答案

  1. # 1 楼答案

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


    只有使用aspect编译器重新编译JDK时,以下操作才有效: 所有线程都是以Thread.start开始的,所以请为该方法编写一个切入点,然后您可以使用建议来做您想做的事情。当然,这并不完美,因为例如,cachedThreadPool执行器可能不会为每个任务启动新线程,但如果在Runnable.runCallable.call而不是Thread.start上注册切入点,这可能就足够了

  2. # 2 楼答案

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

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

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

    2. Listing All Running Threads

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

    起始代码:

    ThreadCreationListener。java

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

    线程创建监视器。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;
    
            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) {
                            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 !
        }
    }
    
  3. # 3 楼答案

    也许你需要一个线程组。所有线程都是一个线程组的成员,当您启动一个新线程时,默认情况下,它会被添加到与其父线程相同的组中

    理论上,在组中添加或删除线程时,可以(但不建议)通知子类

    轮询这些组的线程或轮询所有线程可能是更好的解决方案