Skip to content

Commit efd9f6d

Browse files
committed
Code from book
Also added some 5-second timeouts in various places to build.xml because (expected) exceptions were causing ant to hang for a long time, then inexplicably continue.
1 parent 40d07cc commit efd9f6d

3 files changed

Lines changed: 58 additions & 113 deletions

File tree

concurrency/SynchronizationComparisons.java

Lines changed: 47 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ abstract class Accumulator {
2121
protected String id = "error";
2222
protected final static int SIZE = 100000;
2323
protected static int[] preLoaded = new int[SIZE];
24-
static {
25-
// Load the array of random numbers:
24+
static { // Load the array of random numbers:
2625
Random rand = new Random(47);
2726
for(int i = 0; i < SIZE; i++)
2827
preLoaded[i] = rand.nextInt();
@@ -66,24 +65,14 @@ public void timedTest() {
6665
duration = System.nanoTime() - start;
6766
printf("%-13s: %13d\n", id, duration);
6867
}
69-
public static void
70-
report(Accumulator acc1, Accumulator acc2) {
71-
printf("%-22s: %.2f\n", acc1.id + "/" + acc2.id,
72-
(double)acc1.duration/(double)acc2.duration);
68+
public void report(Accumulator acc2) {
69+
printf("%-22s: %.2f\n", this.id + "/" + acc2.id,
70+
(double)this.duration/(double)acc2.duration);
7371
}
7472
}
7573

76-
class BaseLine extends Accumulator {
77-
{ id = "BaseLine"; }
78-
public void accumulate() {
79-
value += preLoaded[index++];
80-
if(index >= SIZE) index = 0;
81-
}
82-
public long read() { return value; }
83-
}
84-
8574
class SynchronizedTest extends Accumulator {
86-
{ id = "synchronized"; }
75+
{ id = "synch"; }
8776
public synchronized void accumulate() {
8877
value += preLoaded[index++];
8978
if(index >= SIZE) index = 0;
@@ -119,44 +108,44 @@ class AtomicTest extends Accumulator {
119108
{ id = "Atomic"; }
120109
private AtomicInteger index = new AtomicInteger(0);
121110
private AtomicLong value = new AtomicLong(0);
122-
public void accumulate() {
123-
// Oops! Relying on more than one Atomic at
124-
// a time doesn't work. But it still gives us
125-
// a performance indicator:
126-
int i = index.getAndIncrement();
111+
// Relying on more than one Atomic at a time doesn't
112+
// work, so we still have to synchronize. But it gives
113+
// a performance indicator:
114+
public synchronized void accumulate() {
115+
int i;
116+
i = index.getAndIncrement();
127117
value.getAndAdd(preLoaded[i]);
128118
if(++i >= SIZE)
129119
index.set(0);
130120
}
131-
public long read() { return value.get(); }
121+
public synchronized long read() { return value.get(); }
122+
public void report(Accumulator acc2) {
123+
printf("%-22s: %.2f\n", "synch/(Atomic-synch)",
124+
(double)acc2.duration/
125+
((double)this.duration - (double)acc2.duration));
126+
}
132127
}
133128

134129
public class SynchronizationComparisons {
135-
static BaseLine baseLine = new BaseLine();
136130
static SynchronizedTest synch = new SynchronizedTest();
137131
static LockTest lock = new LockTest();
138132
static AtomicTest atomic = new AtomicTest();
139133
static void test() {
140134
print("============================");
141135
printf("%-12s : %13d\n", "Cycles", Accumulator.cycles);
142-
baseLine.timedTest();
143136
synch.timedTest();
144137
lock.timedTest();
145138
atomic.timedTest();
146-
Accumulator.report(synch, baseLine);
147-
Accumulator.report(lock, baseLine);
148-
Accumulator.report(atomic, baseLine);
149-
Accumulator.report(synch, lock);
150-
Accumulator.report(synch, atomic);
151-
Accumulator.report(lock, atomic);
139+
synch.report(lock);
140+
atomic.report(synch);
152141
}
153142
public static void main(String[] args) {
154143
int iterations = 5; // Default
155144
if(args.length > 0) // Optionally change iterations
156145
iterations = new Integer(args[0]);
157146
// The first time fills the thread pool:
158147
print("Warmup");
159-
baseLine.timedTest();
148+
synch.timedTest();
160149
// Now the initial test doesn't include the cost
161150
// of starting the threads for the first time.
162151
// Produce multiple data points:
@@ -166,91 +155,42 @@ public static void main(String[] args) {
166155
}
167156
Accumulator.exec.shutdown();
168157
}
169-
} /* Output: (Sample)
158+
} /* Output: (Sample) using JDK6u10
170159
Warmup
171-
BaseLine : 34237033
160+
synch : 129868038
172161
============================
173162
Cycles : 50000
174-
BaseLine : 20966632
175-
synchronized : 24326555
176-
Lock : 53669950
177-
Atomic : 30552487
178-
synchronized/BaseLine : 1.16
179-
Lock/BaseLine : 2.56
180-
Atomic/BaseLine : 1.46
181-
synchronized/Lock : 0.45
182-
synchronized/Atomic : 0.79
183-
Lock/Atomic : 1.76
163+
synch : 126407922
164+
Lock : 51207369
165+
Atomic : 141845223
166+
synch/Lock : 2.47
167+
synch/(Atomic-synch) : 8.19
184168
============================
185169
Cycles : 100000
186-
BaseLine : 41512818
187-
synchronized : 43843003
188-
Lock : 87430386
189-
Atomic : 51892350
190-
synchronized/BaseLine : 1.06
191-
Lock/BaseLine : 2.11
192-
Atomic/BaseLine : 1.25
193-
synchronized/Lock : 0.50
194-
synchronized/Atomic : 0.84
195-
Lock/Atomic : 1.68
170+
synch : 251174061
171+
Lock : 105338114
172+
Atomic : 279503250
173+
synch/Lock : 2.38
174+
synch/(Atomic-synch) : 8.87
196175
============================
197176
Cycles : 200000
198-
BaseLine : 80176670
199-
synchronized : 5455046661
200-
Lock : 177686829
201-
Atomic : 101789194
202-
synchronized/BaseLine : 68.04
203-
Lock/BaseLine : 2.22
204-
Atomic/BaseLine : 1.27
205-
synchronized/Lock : 30.70
206-
synchronized/Atomic : 53.59
207-
Lock/Atomic : 1.75
177+
synch : 508778006
178+
Lock : 214398402
179+
Atomic : 574464795
180+
synch/Lock : 2.37
181+
synch/(Atomic-synch) : 7.75
208182
============================
209183
Cycles : 400000
210-
BaseLine : 160383513
211-
synchronized : 780052493
212-
Lock : 362187652
213-
Atomic : 202030984
214-
synchronized/BaseLine : 4.86
215-
Lock/BaseLine : 2.26
216-
Atomic/BaseLine : 1.26
217-
synchronized/Lock : 2.15
218-
synchronized/Atomic : 3.86
219-
Lock/Atomic : 1.79
184+
synch : 1027003521
185+
Lock : 428342577
186+
Atomic : 1115667617
187+
synch/Lock : 2.40
188+
synch/(Atomic-synch) : 11.58
220189
============================
221190
Cycles : 800000
222-
BaseLine : 322064955
223-
synchronized : 336155014
224-
Lock : 704615531
225-
Atomic : 393231542
226-
synchronized/BaseLine : 1.04
227-
Lock/BaseLine : 2.19
228-
Atomic/BaseLine : 1.22
229-
synchronized/Lock : 0.47
230-
synchronized/Atomic : 0.85
231-
Lock/Atomic : 1.79
232-
============================
233-
Cycles : 1600000
234-
BaseLine : 650004120
235-
synchronized : 52235762925
236-
Lock : 1419602771
237-
Atomic : 796950171
238-
synchronized/BaseLine : 80.36
239-
Lock/BaseLine : 2.18
240-
Atomic/BaseLine : 1.23
241-
synchronized/Lock : 36.80
242-
synchronized/Atomic : 65.54
243-
Lock/Atomic : 1.78
244-
============================
245-
Cycles : 3200000
246-
BaseLine : 1285664519
247-
synchronized : 96336767661
248-
Lock : 2846988654
249-
Atomic : 1590545726
250-
synchronized/BaseLine : 74.93
251-
Lock/BaseLine : 2.21
252-
Atomic/BaseLine : 1.24
253-
synchronized/Lock : 33.84
254-
synchronized/Atomic : 60.57
255-
Lock/Atomic : 1.79
191+
synch : 2179255097
192+
Lock : 877216314
193+
Atomic : 2371504710
194+
synch/Lock : 2.48
195+
synch/(Atomic-synch) : 11.34
256196
*///:~

concurrency/build.xml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,10 @@
142142
classname="CaptureUncaughtException"
143143
classpath="${java.class.path};${basedir};${basedir}/.."
144144
dir="../concurrency/"
145-
failonerror="true"
146-
fork="true"/>
145+
failonerror="false"
146+
fork="true"
147+
timeout="5000"
148+
/>
147149
</target>
148150

149151
<target name="CarBuilder">
@@ -244,7 +246,8 @@
244246
classpath="${java.class.path};${basedir};${basedir}/.."
245247
dir="../concurrency/"
246248
failonerror="false"
247-
fork="true"/>
249+
fork="true"
250+
timeout="5000" />
248251
<echo message="* Exception was expected *"/>
249252
</target>
250253

@@ -417,7 +420,8 @@
417420
classpath="${java.class.path};${basedir};${basedir}/.."
418421
dir="../concurrency/"
419422
failonerror="false"
420-
fork="true"/>
423+
fork="true"
424+
timeout="5000"/>
421425
<echo message="* Exception was expected *"/>
422426
</target>
423427

@@ -527,8 +531,9 @@
527531
classname="SettingDefaultHandler"
528532
classpath="${java.class.path};${basedir};${basedir}/.."
529533
dir="../concurrency/"
530-
failonerror="true"
531-
fork="true"/>
534+
failonerror="false"
535+
fork="true"
536+
timeout="5000"/>
532537
</target>
533538

534539
<target name="SimpleDaemons">

concurrency/SynchronizationComparisons-original-fixed.java renamed to concurrency/originalSynchCompare/SynchronizationComparisons-original-fixed.javaz

File renamed without changes.

0 commit comments

Comments
 (0)