1 |
|
/* |
2 |
– |
* @test |
3 |
– |
* @synopsis multiple threads using a single lock |
4 |
– |
*/ |
5 |
– |
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
< |
* Expert Group and released to the public domain. Use, modify, and |
4 |
< |
* redistribute this code in any way without acknowledgement. |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/licenses/publicdomain |
5 |
|
*/ |
6 |
|
|
7 |
|
import java.util.concurrent.*; |
16 |
|
|
17 |
|
public static void main(String[] args) throws Exception { |
18 |
|
int maxThreads = 100; |
19 |
< |
if (args.length > 0) |
19 |
> |
if (args.length > 0) |
20 |
|
maxThreads = Integer.parseInt(args[0]); |
21 |
|
|
22 |
|
print = true; |
48 |
|
} |
49 |
|
|
50 |
|
final void test() throws Exception { |
51 |
< |
for (int i = 0; i < nthreads; ++i) |
51 |
> |
for (int i = 0; i < nthreads; ++i) |
52 |
|
pool.execute(this); |
53 |
|
barrier.await(); |
54 |
|
barrier.await(); |
55 |
|
if (print) { |
56 |
|
long time = timer.getTime(); |
57 |
< |
long tpi = time / ((long)iters * nthreads); |
57 |
> |
long tpi = time / ((long) iters * nthreads); |
58 |
|
System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per lock"); |
59 |
< |
double secs = (double)(time) / 1000000000.0; |
59 |
> |
double secs = (double) time / 1000000000.0; |
60 |
|
System.out.println("\t " + secs + "s run time"); |
61 |
|
} |
62 |
|
|
68 |
|
public final void run() { |
69 |
|
final ReentrantLock lock = this.lock; |
70 |
|
try { |
71 |
< |
barrier.await(); |
71 |
> |
barrier.await(); |
72 |
|
int sum = v + 1; |
73 |
|
int x = 0; |
74 |
|
int n = iters; |
79 |
|
if (k > 0) { |
80 |
|
x = v; |
81 |
|
while (k-- > 0) |
82 |
< |
x = LoopHelpers.compute4(x); |
82 |
> |
x = LoopHelpers.compute6(x); |
83 |
|
v = x; |
84 |
|
} |
85 |
|
else x = sum + 1; |
88 |
|
if ((x += readBarrier) == 0) |
89 |
|
++readBarrier; |
90 |
|
for (int l = x & 7; l > 0; --l) |
91 |
< |
sum += LoopHelpers.compute4(sum); |
92 |
< |
} |
91 |
> |
sum += LoopHelpers.compute6(sum); |
92 |
> |
} |
93 |
|
barrier.await(); |
94 |
|
result += sum; |
95 |
|
} |
96 |
< |
catch (Exception ie) { |
97 |
< |
return; |
96 |
> |
catch (Exception ie) { |
97 |
> |
return; |
98 |
|
} |
99 |
|
} |
100 |
|
} |