线程创建监听器 [英] Thread creation listener

查看:106
本文介绍了线程创建监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以用Java编写线程创建侦听器?例如使用aop?!

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.

推荐答案

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

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.

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

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.

这篇关于线程创建监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆