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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I think this would be possible with AOP (aspectj for instance). But it is still required to create your own
ThreadandThreadGroup/Executortypes, unless you can recompile the JDK classes with the aspect compiler. Define the pointcut on your thread’sstartmethod if you want to register on thread launching or on thecreateThreadof 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 onRunnable.runandCallable.callrather than onThread.start, that might be sufficient enough.