summaryrefslogtreecommitdiffstats
path: root/tests/features152/synchronization/Useful2.java
blob: 46fc82d5af7d66feaacf8ca8297232fe0cbfe127 (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
// Exploring synchronization

aspect LockMonitor {
	long locktimer = 0;
	int iterations = 0;
	Object currentObject = null;
	long activeTimer;
	
	before(Useful2 o ): lock() && args(o) {
		activeTimer = System.currentTimeMillis();
		currentObject = o;
	}
	
	after(Useful2 o ): unlock() && args(o) {
		if (o!=currentObject) { 
			throw new RuntimeException("Unlocking on incorrect thing?!?");
		}
		if (activeTimer!=0) {
			locktimer+=(System.currentTimeMillis()-activeTimer);
			iterations++;
			activeTimer=0;
		}
	}
	
	after() returning: execution(* main(..)) {
		System.err.println("Average time spent with lock over "+iterations+" iterations is "+
				(((double)locktimer)/
				 ((double)iterations))+"ms");
	}
}

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

}