Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 线程创建侦听器_Java_Multithreading_Jvm_Classloader - Fatal编程技术网

Java 线程创建侦听器

Java 线程创建侦听器,java,multithreading,jvm,classloader,Java,Multithreading,Jvm,Classloader,可以用java编写线程创建侦听器吗?例如使用aop 我的意思是,如果我的应用程序创建了一个线程,我希望在我自己的表、容器或其他东西中注册这个对象。我将创建一个线程,该线程连续列出JVM上所有正在运行的线程。 然后,每当它注意到新线程出现时,它都会以任何一种方式通知代码中的类 以下是一些关于如何列出JVM上当前运行的所有线程的链接: ============ 起始代码: ThreadCreationListener.java public interface ThreadCreationLis

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


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

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

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

  • ============

    起始代码:

    ThreadCreationListener.java

    public interface ThreadCreationListener {
        public void onThreadCreation(Thread newThread);
    }
    
    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;
       }
    
    public class MyThreadInfoConsumer implements ThreadCreationListener {
        public void onThreadCreation(Thread newThread) {
            // Process here the notification...
        }
    }
    
    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 !
        }
    }
    
    ThreadCreationMonitor.java

    public interface ThreadCreationListener {
        public void onThreadCreation(Thread newThread);
    }
    
    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;
       }
    
    public class MyThreadInfoConsumer implements ThreadCreationListener {
        public void onThreadCreation(Thread newThread) {
            // Process here the notification...
        }
    }
    
    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 !
        }
    }
    
    Main.java

    public interface ThreadCreationListener {
        public void onThreadCreation(Thread newThread);
    }
    
    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;
       }
    
    public class MyThreadInfoConsumer implements ThreadCreationListener {
        public void onThreadCreation(Thread newThread) {
            // Process here the notification...
        }
    }
    
    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 !
        }
    }
    

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


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

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

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


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

    如果在主线程中注册
    类加载器,希望所有新线程都会继承该类,并且希望任何线程都需要加载某些类,这意味着将咨询您的
    类加载器
    ,您有机会注册线程……我将使用侦听器所需的方法扩展
    线程
    。当然,这只适用于您的代码。您是否需要跟踪转换(例如:“现在已经创建了一个新线程,我必须立即对其作出反应”),或者在特定的时间点在应用程序中创建一个或一些线程就够了(例如:“现在我想知道这些线程并对它们做些什么。”)?我想写我自己的线程监视器,所以我需要即时的“当前”线程对象。我将创建自己的装饰线程来处理所有这些监控内容,但问题是如何在不覆盖任何内容的情况下获得新线程将要创建的信息——“立即”。我想写一个易于在任何应用程序中运行的监视器。请查看编写eclipse调试器的人员。也许他们可以给这个问题一个提示;)这些例子都是好的和有用的,但是没有听众的行为。我想知道jvm将创建新线程的信息,比如说“Instatnly”,而不重写任何内容。@ukaszRzeszotarski您可以从上面的示例中实现侦听器行为,但它们是关于运行线程而不是创建的线程(如您所问)。这可能适用于运行线程,但是,问题是关于侦听创建的线程而不是运行的线程。@user454322否。当线程启动时,将调用before通知。(即调用这3个方法中的任何一个)我知道它将在线程启动之前调用,但在创建线程时调用?e、 例如,如果创建了一个线程但从未启动,会发生什么?@user454322。我明白了,我把这个问题解释为他想注册“线程启动”事件。我编辑了我的答案。@zeller听起来不错。那么在线程类构造函数上注册切入点呢?像这样的东西行得通吗?你怎么看?一个线程是在创建时或启动时添加到线程组的?从代码上看,它建议何时启动。如果您想知道线程是何时创建的,您需要对线程进行子类化(并更改所有线程创建代码)