author | dl |
Tue, 16 Jan 2018 18:28:39 -0800 | |
changeset 48541 | 946e34c2dec9 |
parent 47216 | 71c04702a3d5 |
child 51754 | 594919232b8f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
6 |
* published by the Free Software Foundation. |
|
7 |
* |
|
8 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
9 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
10 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
11 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
12 |
* accompanied this code). |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License version |
|
15 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
16 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17 |
* |
|
5506 | 18 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
19 |
* or visit www.oracle.com if you need additional information or have any |
|
20 |
* questions. |
|
2 | 21 |
*/ |
22 |
||
23 |
/* |
|
24 |
* This file is available under and governed by the GNU General Public |
|
25 |
* License version 2 only, as published by the Free Software Foundation. |
|
26 |
* However, the following notice accompanied the original version of this |
|
27 |
* file: |
|
28 |
* |
|
29 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
30 |
* Expert Group and released to the public domain, as explained at |
|
9242
ef138d47df58
7034657: Update Creative Commons license URL in legal notices
dl
parents:
7518
diff
changeset
|
31 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
2 | 32 |
*/ |
33 |
||
34 |
/* |
|
35 |
* @test |
|
36 |
* @bug 4965960 |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
35981
diff
changeset
|
37 |
* @summary Exercise ExecutorCompletionService |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
35981
diff
changeset
|
38 |
* @library /lib/testlibrary/ |
2 | 39 |
*/ |
40 |
||
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
35981
diff
changeset
|
41 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
34347 | 42 |
|
43 |
import java.util.concurrent.Callable; |
|
44 |
import java.util.concurrent.ExecutorCompletionService; |
|
45 |
import java.util.concurrent.ExecutorService; |
|
46 |
import java.util.concurrent.Executors; |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
35981
diff
changeset
|
47 |
import jdk.testlibrary.Utils; |
2 | 48 |
|
49 |
public class ExecutorCompletionServiceLoops { |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
35981
diff
changeset
|
50 |
static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000); |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
35981
diff
changeset
|
51 |
static final int POOLSIZE = 10; |
2 | 52 |
static final ExecutorService pool = |
53 |
Executors.newFixedThreadPool(POOLSIZE); |
|
54 |
static final ExecutorCompletionService<Integer> ecs = |
|
48541
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
55 |
new ExecutorCompletionService<>(pool); |
2 | 56 |
static boolean print = false; |
57 |
||
58 |
public static void main(String[] args) throws Exception { |
|
59 |
int max = 8; |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
35981
diff
changeset
|
60 |
int base = 2000; |
2 | 61 |
|
62 |
if (args.length > 0) |
|
63 |
max = Integer.parseInt(args[0]); |
|
64 |
||
65 |
System.out.println("Warmup..."); |
|
35981
e3e89c0bb3d9
8145485: Miscellaneous changes imported from jsr166 CVS 2016-02
dl
parents:
34347
diff
changeset
|
66 |
oneTest(base); |
2 | 67 |
print = true; |
68 |
||
69 |
for (int i = 1; i <= max; i += (i+1) >>> 1) { |
|
70 |
System.out.print("n: " + i * base); |
|
35981
e3e89c0bb3d9
8145485: Miscellaneous changes imported from jsr166 CVS 2016-02
dl
parents:
34347
diff
changeset
|
71 |
oneTest(i * base); |
2 | 72 |
} |
73 |
pool.shutdown(); |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
35981
diff
changeset
|
74 |
if (! pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS)) |
2 | 75 |
throw new Error(); |
76 |
} |
|
77 |
||
78 |
static class Task implements Callable<Integer> { |
|
79 |
public Integer call() { |
|
80 |
int s = 0; |
|
81 |
int l = System.identityHashCode(this); |
|
82 |
for (int i = 0; i < 5; ++i) { |
|
83 |
l = LoopHelpers.compute2(l); |
|
84 |
s += LoopHelpers.compute1(l); |
|
85 |
} |
|
86 |
return new Integer(s); |
|
87 |
} |
|
88 |
} |
|
89 |
||
90 |
static class Producer implements Runnable { |
|
91 |
final ExecutorCompletionService cs; |
|
92 |
final int iters; |
|
93 |
Producer(ExecutorCompletionService ecs, int i) { |
|
94 |
cs = ecs; |
|
95 |
iters = i; |
|
96 |
} |
|
97 |
public void run() { |
|
98 |
for (int i = 0; i < iters; ++i) |
|
99 |
ecs.submit(new Task()); |
|
100 |
} |
|
101 |
} |
|
102 |
||
103 |
static void oneTest(int iters) throws Exception { |
|
104 |
long startTime = System.nanoTime(); |
|
105 |
new Thread(new Producer(ecs, iters)).start(); |
|
106 |
||
107 |
int r = 0; |
|
108 |
for (int i = 0; i < iters; ++i) |
|
109 |
r += ecs.take().get().intValue(); |
|
110 |
||
111 |
long elapsed = System.nanoTime() - startTime; |
|
112 |
long tpi = elapsed/ iters; |
|
113 |
||
114 |
if (print) |
|
115 |
System.out.println("\t: " + LoopHelpers.rightJustify(tpi) + " ns per task"); |
|
116 |
||
117 |
if (r == 0) // avoid overoptimization |
|
118 |
System.out.println("useless result: " + r); |
|
119 |
} |
|
120 |
||
121 |
} |