Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-dev] A little help with perthis/pertarget ...

If your threads are each using a different instance then perthis is
what you want.  If each of your threads might be exercising the same
instance, perthis() will still result in the start time being splatted
each time a new thread enters the woven code (you are saying you want
a different aspect instance per 'this', so every thread will be
sharing the same aspect instance).

If you want a different aspect instance per execution of the methods
and the threads may share an instance, then use percflow() instead.

In the code below the timers are sometimes wrongly reported due to the
shared instance across all threads:

aspect ProfileAspect perthis(execution(* *Service.foo (..))) {

    private long start;

    pointcut services(): (execution(* *Service.foo (..)));

    before(): services() {
      start = System.currentTimeMillis();
    }

    after(): services() {
      // Should always report 50+ but will report <50 if perthis is
used with a shared instance
      System.err.println(thisJoinPoint+"
timetorun="+Long.toString(System.currentTimeMillis()-start));
    }

}


public class CService implements Runnable {

  public static void main(String[]argv) {
    CService cs = new CService();
    for (int i=0;i<100;i++) {
      new Thread(cs).start(); // all threads using same 'this'
    }
  }

  public void run() {
    for (int i=0;i<10;i++) { foo(); }
  }

  public void foo() {
      try { Thread.sleep(50); } catch (Exception e) {}
  }

}

Change perthis to percflow and everything in the aspect and all timers
will be correct.

If the problem is that you just don't seem to be executing the advice,
I would change to using System.out rather than LOG to see where it is
going since it ought to be running based on your woven code.

Andy.


Back to the top