Thanks a lot Alex. That's exactly the same way I'm too looking into.
I'm just playing around to sort of get parent/child relationship between
the threads. Using this, I could able to hook into before and after
pointcuts, to assign Thread names to keep the Parent/Child relationship
going on. Yes, as you told there are lot of cases to look for, but I guess
its a good starting point for me.
On Sat, Jul 23, 2016 at 2:21 PM, Alexander Kriegisch-2 [via AspectJ] <
[email protected]> wrote:
> I did not answer right away because I was busy, sorry. I think that the
> answer is a little bit more complex because maybe you do not know exactly
> how your 3rd party application or library handles concurrency. Maybe it
> directly creates threads, maybe it uses thread pools. Then possibly you
> rather want to intercept when threads are started than when they are
> created. Maybe the library uses an ExecutorService or - even worse - a
> ScheduledExecutorService. Unless you really weave into execution joinpoints
> within the JDK there is no way to capture 100% of all the thread/task
> creation/starting events. What makes things worse is that it is kinda hard
> to hook into Runnable creation because mostly anonymous subclasses or even
> lambdas (like in my example) are used. Please use the DEBUG switch in my
> sample aspect in order to see in more detail what is going on.
>
> Let us assume your third party code is like this:
>
>
>
> package org.thirdparty;
>
> import java.util.ArrayList;
> import java.util.List;
> import java.util.concurrent.Callable;
> import java.util.concurrent.ExecutionException;
> import java.util.concurrent.ExecutorService;
> import java.util.concurrent.Executors;
> import java.util.concurrent.ScheduledExecutorService;
> import java.util.concurrent.TimeUnit;
>
> public class Application {
> public static void main(String[] args) throws InterruptedException,
> ExecutionException {
> new Thread(() -> System.out.println("Thread")).start();
>
> Runnable r1 = () -> System.out.println("Runnable 1");
> Runnable r2 = () -> System.out.println("Runnable 2");
> ExecutorService executor = Executors.newCachedThreadPool();
> executor.execute(r1);
> executor.execute(r2);
> Thread.sleep(500);
> executor.execute(r1);
> executor.execute(r2);
> Thread.sleep(500);
>
> Callable<Integer> task = () -> { System.out.println("Callable task");
> return 11; };
> executor.submit(task);
>
> List<Callable<Integer>> tasks = new ArrayList<>();
> tasks.add(task);
> tasks.add(task);
> executor.invokeAll(tasks);
> executor.invokeAny(tasks);
> executor.shutdown();
>
> ScheduledExecutorService scheduler =
> Executors.newScheduledThreadPool(1);
> scheduler.scheduleAtFixedRate(() -> System.out.println("Scheduled
> task"), 0, 250, TimeUnit.MILLISECONDS);
> Thread.sleep(1000);
> scheduler.shutdown();
> }
> }
>
> As you can see, several types of concurrency tools are used here. Now here
> is an aspect trying to capture what is going on:
>
> package de.scrum_master.aspect;
>
> import java.util.concurrent.ExecutorService;
> import java.util.concurrent.ScheduledExecutorService;
>
> public aspect ThreadInterceptor {
> private static boolean DEBUG = false;
> before() :
> if(!DEBUG) && (
> call(* Thread+.start()) ||
> call(* ExecutorService+.execute(..)) ||
> call(* ExecutorService+.submit(..)) ||
> call(* ExecutorService+.invokeAll(..)) ||
> call(* ExecutorService+.invokeAny(..)) ||
> call(* ScheduledExecutorService.scheduleAtFixedRate(..))
> )
> {
> System.out.println(thisJoinPoint);
> }
>
> before() :
> if(DEBUG) &&
> within(*) &&
> !within(ThreadInterceptor) &&
> !get(* *) &&
> !call(* println(..))
> {
> System.out.println(thisJoinPoint);
> }
> }
>
> The first pointcut/advice shows where you need to hook into in order to
> capture the most obvious events. Try the second one to see more. The output
> for the first one looks like this:
>
> call(void java.lang.Thread.start())
> Thread
> call(void java.util.concurrent.ExecutorService.execute(Runnable))
> call(void java.util.concurrent.ExecutorService.execute(Runnable))
> Runnable 1
> Runnable 2
> call(void java.util.concurrent.ExecutorService.execute(Runnable))
> call(void java.util.concurrent.ExecutorService.execute(Runnable))
> Runnable 1
> Runnable 2
> call(Future java.util.concurrent.ExecutorService.submit(Callable))
> call(List java.util.concurrent.ExecutorService.invokeAll(Collection))
> Callable task
> Callable task
> Callable task
> call(Object java.util.concurrent.ExecutorService.invokeAny(Collection))
> Callable task
> Callable task
> call(ScheduledFuture
> java.util.concurrent.ScheduledExecutorService.scheduleAtFixedRate(Runnable,
> long, long, TimeUnit))
> Scheduled task
> Scheduled task
> Scheduled task
> Scheduled task
>
> Please note how the aspect is unable to capture each single scheduled task
> execution but only captured the scheduling start event instead.
>
> Feel free to experiment and come up with a better solution. I was just
> curious and playing around.
>
> --
> Alexander Kriegisch
> https://scrum-master.de
>
>
>
> Andy Clement schrieb am 23.07.2016 01:45:
>
>
> aspect X {
> before(): call(Thread.new(..)) { // Thread constructor called
> System.out.println("Thread created!");
> }
> before(): call(* Thread.start(..)) { // Thread.start called
> System.out.println("Thread started!");
> }
> }
>
> Andy
>
>
> On Jul 20, 2016, at 1:07 AM, ants <[hidden email]
> <http:///user/SendEmail.jtp?type=node&node=4652116&i=0>> wrote:
>
>
>
> On Wed, Jul 20, 2016 at 1:03 PM, Alexander Kriegisch-2 [via AspectJ] <<a
> href="x-msg://16/user/SendEmail.jtp?type=node&node=4652110&i=0"
> link="external" rel="nofollow" target="_top">[hidden email]> wrote:
>>
>> You can easily hook into places where your own code creates threads.
>>
>> If you need to hook into threads created by third party libs, you need to
>> weave their binaries via post-compile or load-time weaving.
>
>
> Can you give some examples for it with respect to weaving thread creation?
>
>
>>
>>
>> Theoretically you can also weave into the JDK, creating your own
>> tools.jar with woven classes, but probably you do not want to go that far.
>> I have done that in the past just for the fun of it.
>> --
>> Alexander Kriegisch
>> https://scrum-master.de
>>
>>
>> > Am 20.07.2016 um 05:42 schrieb ants <[hidden email]
>> <http://user/SendEmail.jtp?type=node&node=4652109&i=0>>:
>> >
>> > Hi All,
>> >
>> > Is there a way to hook to thread creations and destroy from AspectJ
>> > pointcuts?
>> >
>> > ` Anto.
>> >
>> >
>> >
>> > --
>> > View this message in context:
>> http://aspectj.2085585.n4.nabble.com/Hook-Thread-Creations-tp4652108.html
>> > Sent from the AspectJ - users mailing list archive at Nabble.com
>> <http://nabble.com>.
>> > _______________________________________________
>> > aspectj-users mailing list
>> > [hidden email] <http://user/SendEmail.jtp?type=node&node=4652109&i=1>
>> > To change your delivery options, retrieve your password, or unsubscribe
>> from this list, visit
>> > https://dev.eclipse.org/mailman/listinfo/aspectj-users
>>
>> _______________________________________________
>> aspectj-users mailing list
>> [hidden email] <http://user/SendEmail.jtp?type=node&node=4652109&i=2>
>> To change your delivery options, retrieve your password, or unsubscribe
>> from this list, visit
>> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>>
>> <img src="data:image/gif;base64,"> *smime.p7s* (3K) Download Attachment
>> <http://aspectj.2085585.n4.nabble.com/attachment/4652109/0/smime.p7s>
>>
>> ------------------------------
>> If you reply to this email, your message will be added to the discussion
>> below:
>>
>> http://aspectj.2085585.n4.nabble.com/Hook-Thread-Creations-tp4652108p4652109.html
>> To unsubscribe from Hook Thread Creations, click here.
>> NAML
>> <http://aspectj.2085585.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>>
>
> ------------------------------
> View this message in context: Re: Hook Thread Creations
> <http://aspectj.2085585.n4.nabble.com/Hook-Thread-Creations-tp4652108p4652110.html>
> Sent from the AspectJ - users mailing list archive
> <http://aspectj.2085585.n4.nabble.com/AspectJ-users-f2077686.html> at
> Nabble.com <http://nabble.com>.
> _______________________________________________
> aspectj-users mailing list
> [hidden email] <http:///user/SendEmail.jtp?type=node&node=4652116&i=1>
> To change your delivery options, retrieve your password, or unsubscribe
> from this list, visit
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>
>
> _______________________________________________
> aspectj-users mailing list
> [hidden email] <http:///user/SendEmail.jtp?type=node&node=4652116&i=2>
> To change your delivery options, retrieve your password, or unsubscribe
> from this list, visit
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>
> *Application.java* (1K) Download Attachment
> <http://aspectj.2085585.n4.nabble.com/attachment/4652116/0/Application.java>
> *ThreadInterceptor.aj* (865 bytes) Download Attachment
> <http://aspectj.2085585.n4.nabble.com/attachment/4652116/1/ThreadInterceptor.aj>
>
>
> ------------------------------
> If you reply to this email, your message will be added to the discussion
> below:
>
> http://aspectj.2085585.n4.nabble.com/Hook-Thread-Creations-tp4652108p4652116.html
> To unsubscribe from Hook Thread Creations, click here
> <http://aspectj.2085585.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4652108&code=YW50by5hcmF2aW50aC5jc2VAZ21haWwuY29tfDQ2NTIxMDh8LTE5Mjg4ODk5NTM=>
> .
> NAML
> <http://aspectj.2085585.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>
--
View this message in context:
http://aspectj.2085585.n4.nabble.com/Hook-Thread-Creations-tp4652108p4652117.html
Sent from the AspectJ - users mailing list archive at Nabble.com._______________________________________________
aspectj-users mailing list
[email protected]
To change your delivery options, retrieve your password, or unsubscribe from
this list, visit
https://dev.eclipse.org/mailman/listinfo/aspectj-users