ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/SimpleSemaphoreLoops.java
(Generate patch)

Comparing jsr166/src/test/loops/SimpleSemaphoreLoops.java (file contents):
Revision 1.2 by dl, Fri Jun 10 15:45:19 2005 UTC vs.
Revision 1.8 by jsr166, Sat Dec 31 19:05:06 2016 UTC

# Line 1 | Line 1
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();
# Line 20 | Line 17 | public final class SimpleSemaphoreLoops
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;
# Line 52 | Line 49 | public final class SimpleSemaphoreLoops
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  
# Line 72 | Line 69 | public final class SimpleSemaphoreLoops
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;
# Line 91 | Line 88 | public final class SimpleSemaphoreLoops
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      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines