Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] multiple aspect creation using perthis association and threads

Hi,

I think this is https://bugs.eclipse.org/bugs/show_bug.cgi?id=318878 "Pertarget aspect instantiation is not thread-safe". Although you are using perthis, it is the same problem. The bind method generated by AJ is not synchronized. You should be getting one aspect instance. I modified AspectJ to make it synchronized and it behaves as expected - one instance. I'll see if the test suite likes the change and commit it.

cheers,
Andy


On 21 February 2013 10:04, Thomas Introini <introt@xxxxxxxxx> wrote:
Hello,

I'm having some trouble understanding aspectj's behavior regarding the "perthis" clause using threads. In particular, it seems that sometimes aspectj, capturing a join point executed in a different thread, creates a new aspect despite the fact that this join point refers to an object already associated with an existing aspect. 
I've created a simple program to show this particular issue i'm having.

In Main.java i have this main method:
public static void main(String[] args) {
Foo f=new Foo();
for(int i=0;i<5;i++){
new Thread(f).start();
}

}
which creates 5 different threads, using the same Foo object.

The Foo.java contains:

public class Foo implements Runnable{
public void run(){
System.out.println("foo "+this);
}
}

In FooA.aj i have the following aspect:

   public aspect FooA perthis(fooExc()) {
public FooA(){
System.out.println("new aspect : "+this);
}
public pointcut fooExc():execution(void Foo.run());
before(): fooExc(){
System.out.println("Inside "+this);
}
   }

which should create only one aspect executing the previous main.
I'm writing this mail because i'm experiencing different results every time i execute this sample program.
For instance, sometimes i get this results:

new aspect : FooA@a1807c
Inside FooA@a1807c
foo Foo@fa7e74
Inside FooA@a1807c
foo Foo@fa7e74
Inside FooA@a1807c
foo Foo@fa7e74
Inside FooA@a1807c
foo Foo@fa7e74
Inside FooA@a1807c
foo Foo@fa7e74

Only one aspect is created since there is only one Foo object.
Sometimes the program creates an aspect for every thread:

new aspect : FooA@a1807c
Inside FooA@a1807c
foo Foo@133796
new aspect : FooA@82c01f
new aspect : FooA@fa7e74
Inside FooA@fa7e74
foo Foo@133796
new aspect : FooA@e102dc
new aspect : FooA@183f74d
Inside FooA@183f74d
foo Foo@133796
Inside FooA@e102dc
foo Foo@133796
Inside FooA@82c01f
foo Foo@133796

as shown five aspects are created.
And some other times this happens:

new aspect : FooA@e102dc
new aspect : FooA@82c01f
Inside FooA@e102dc
new aspect : FooA@183f74d
Inside FooA@183f74d
foo Foo@133796
foo Foo@133796
Inside FooA@82c01f
Inside FooA@e102dc
foo Foo@133796
foo Foo@133796
Inside FooA@183f74d
foo Foo@133796

where three different aspects are created.
I'm currently compiling and executing on Ubuntu 12.04 using aspectj 1.7.2 and Java 6 with these commands:
ajc -cp .:./aspectjrt.jar -d ./out -sourceroots . -aspectpath .
java -cp .:../aspectjrt.jar Main

Is the program's behavior correct creating one aspect for each thread execution? or just one aspect is supposed to be created?

Thanks for your time.

Thomas.

--
I.T.

_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users



Back to the top