OnJava8-Examples/lowlevel/SynchronizedComparison.java

88 lines
2.0 KiB
Java
Raw Permalink Normal View History

2017-01-14 00:34:41 -08:00
// lowlevel/SynchronizedComparison.java
// (c)2021 MindView LLC: see Copyright.txt
2017-01-14 00:34:41 -08:00
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Synchronizing blocks instead of entire methods
// speeds up access.
import java.util.*;
import java.util.stream.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import onjava.Nap;
abstract class Guarded {
AtomicLong callCount = new AtomicLong();
public abstract void method();
@Override public String toString() {
2017-01-14 00:34:41 -08:00
return getClass().getSimpleName() +
": " + callCount.get();
}
}
class SynchronizedMethod extends Guarded {
@Override public synchronized void method() {
2017-01-22 16:48:11 -08:00
new Nap(0.01);
2017-01-14 00:34:41 -08:00
callCount.incrementAndGet();
}
}
class CriticalSection extends Guarded {
@Override public void method() {
2017-01-22 16:48:11 -08:00
new Nap(0.01);
2017-01-14 00:34:41 -08:00
synchronized(this) {
callCount.incrementAndGet();
}
}
}
class Caller implements Runnable {
private Guarded g;
2017-05-01 14:33:10 -06:00
Caller(Guarded g) { this.g = g; }
2017-01-14 15:50:24 -08:00
private AtomicLong successfulCalls =
new AtomicLong();
2017-01-14 00:34:41 -08:00
private AtomicBoolean stop =
new AtomicBoolean(false);
@Override public void run() {
2017-01-18 16:10:15 -08:00
new Timer().schedule(new TimerTask() {
public void run() { stop.set(true); }
}, 2500);
2017-01-14 15:50:24 -08:00
while(!stop.get()) {
2017-01-14 00:34:41 -08:00
g.method();
2017-01-14 15:50:24 -08:00
successfulCalls.getAndIncrement();
}
System.out.println(
"-> " + successfulCalls.get());
2017-01-14 00:34:41 -08:00
}
}
public class SynchronizedComparison {
static void test(Guarded g) {
List<CompletableFuture<Void>> callers =
Stream.of(
new Caller(g),
new Caller(g),
new Caller(g),
new Caller(g))
.map(CompletableFuture::runAsync)
.collect(Collectors.toList());
callers.forEach(CompletableFuture::join);
System.out.println(g);
}
public static void main(String[] args) {
test(new CriticalSection());
test(new SynchronizedMethod());
}
}
/* Output:
-> 159
-> 159
-> 159
-> 159
CriticalSection: 636
-> 65
-> 21
-> 11
-> 68
SynchronizedMethod: 165
2017-01-14 00:34:41 -08:00
*/