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/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
< |
import java.util.concurrent.*; |
8 |
< |
import java.util.concurrent.locks.*; |
9 |
< |
import java.util.*; |
7 |
> |
import java.util.concurrent.CyclicBarrier; |
8 |
> |
import java.util.concurrent.ExecutorService; |
9 |
> |
import java.util.concurrent.Executors; |
10 |
> |
import java.util.concurrent.Semaphore; |
11 |
|
|
12 |
|
public final class SimpleSemaphoreLoops { |
13 |
|
static final ExecutorService pool = Executors.newCachedThreadPool(); |
17 |
|
|
18 |
|
public static void main(String[] args) throws Exception { |
19 |
|
int maxThreads = 100; |
20 |
< |
if (args.length > 0) |
20 |
> |
if (args.length > 0) |
21 |
|
maxThreads = Integer.parseInt(args[0]); |
22 |
|
|
23 |
|
print = true; |
49 |
|
} |
50 |
|
|
51 |
|
final void test() throws Exception { |
52 |
< |
for (int i = 0; i < nthreads; ++i) |
52 |
> |
for (int i = 0; i < nthreads; ++i) |
53 |
|
pool.execute(this); |
54 |
|
barrier.await(); |
55 |
|
barrier.await(); |
56 |
|
if (print) { |
57 |
|
long time = timer.getTime(); |
58 |
< |
long tpi = time / ((long)iters * nthreads); |
58 |
> |
long tpi = time / ((long) iters * nthreads); |
59 |
|
System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per lock"); |
60 |
< |
double secs = (double)(time) / 1000000000.0; |
60 |
> |
double secs = (double) time / 1000000000.0; |
61 |
|
System.out.println("\t " + secs + "s run time"); |
62 |
|
} |
63 |
|
|
69 |
|
public final void run() { |
70 |
|
final Semaphore lock = this.lock; |
71 |
|
try { |
72 |
< |
barrier.await(); |
72 |
> |
barrier.await(); |
73 |
|
int sum = v + 1; |
74 |
|
int x = 0; |
75 |
|
int n = iters; |
88 |
|
++readBarrier; |
89 |
|
for (int l = x & 7; l > 0; --l) |
90 |
|
sum += LoopHelpers.compute6(sum); |
91 |
< |
} |
91 |
> |
} |
92 |
|
barrier.await(); |
93 |
|
result += sum; |
94 |
|
} |
95 |
< |
catch (Exception ie) { |
96 |
< |
return; |
95 |
> |
catch (Exception ie) { |
96 |
> |
return; |
97 |
|
} |
98 |
|
} |
99 |
|
} |