summaryrefslogtreecommitdiffstats
path: root/tests/features152/synchronization/Useful1.java
blob: d8ecbe3a065d0257dc7a9af34f66c3a162778271 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Exploring synchronization

aspect WithinAspect {
	long locktimer = 0;
	int iterations = 0;
	Object currentObject = null;
	boolean didSomething = false;
	long activeTimer;
	
	before(Object o ): within(Useful1) && args(o) {
		if (thisJoinPoint.getSignature().toString().startsWith("lock(")) {
			activeTimer = System.currentTimeMillis();
			didSomething = true;
		}
	}
	
	after(Object o ): within(Useful1) && args(o) {
		if (thisJoinPoint.getSignature().toString().startsWith("unlock(")) {
			if (activeTimer!=0) {
				locktimer+=(System.currentTimeMillis()-activeTimer);
				iterations++;
				activeTimer=0;
				didSomething = true;
			}
		}
	}
	
	after() returning: execution(* main(..)) {
		System.err.println("Average lock taking time over "+iterations+" iterations is "+
				(((double)locktimer)/
				 ((double)iterations))+"ms");
		if (didSomething) System.err.println("We did time something!"); // can write a test looking for this line, it won't vary
	}
}

public class Useful1 {
	public static void main(String[] args) {
		Useful1 u = new Useful1();
		
		for (int i = 0; i < 2000; i++) {
			u.methodWithSynchronizedBlock();
		}
	}
	
	public void methodWithSynchronizedBlock() {
		synchronized (this) {
			for (int ii=0;ii<100;ii++);
		}
	}

}