finishing
This commit is contained in:
parent
a179863adb
commit
3561018090
@ -39,6 +39,8 @@ class CriticalSection extends Guarded {
|
|||||||
class Caller implements Runnable {
|
class Caller implements Runnable {
|
||||||
private Guarded g;
|
private Guarded g;
|
||||||
public Caller(Guarded g) { this.g = g; }
|
public Caller(Guarded g) { this.g = g; }
|
||||||
|
private AtomicLong successfulCalls =
|
||||||
|
new AtomicLong();
|
||||||
private AtomicBoolean stop =
|
private AtomicBoolean stop =
|
||||||
new AtomicBoolean(false);
|
new AtomicBoolean(false);
|
||||||
class Stop extends TimerTask {
|
class Stop extends TimerTask {
|
||||||
@ -48,8 +50,12 @@ class Caller implements Runnable {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
new Timer().schedule(new Stop(), 2500);
|
new Timer().schedule(new Stop(), 2500);
|
||||||
while(!stop.get())
|
while(!stop.get()) {
|
||||||
g.method();
|
g.method();
|
||||||
|
successfulCalls.getAndIncrement();
|
||||||
|
}
|
||||||
|
System.out.println(
|
||||||
|
"-> " + successfulCalls.get());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,6 +78,14 @@ public class SynchronizedComparison {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* Output:
|
/* Output:
|
||||||
CriticalSection: 972
|
-> 152
|
||||||
SynchronizedMethod: 247
|
-> 152
|
||||||
|
-> 153
|
||||||
|
-> 153
|
||||||
|
CriticalSection: 610
|
||||||
|
-> 44
|
||||||
|
-> 28
|
||||||
|
-> 62
|
||||||
|
-> 24
|
||||||
|
SynchronizedMethod: 158
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user