1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
+ |
import java.util.*; |
8 |
|
import java.util.concurrent.*; |
8 |
– |
import java.util.concurrent.locks.*; |
9 |
|
import java.util.concurrent.atomic.*; |
10 |
< |
import java.util.*; |
10 |
> |
import java.util.concurrent.locks.*; |
11 |
|
|
12 |
|
public final class ContextSwitchTest { |
13 |
< |
final static int iters = 1000000; |
13 |
> |
static final int iters = 1000000; |
14 |
|
static AtomicReference turn = new AtomicReference(); |
15 |
|
public static void main(String[] args) throws Exception { |
16 |
|
test(); |
31 |
|
b.join(); |
32 |
|
long endTime = System.nanoTime(); |
33 |
|
int np = a.nparks + b.nparks; |
34 |
< |
System.out.println("Average time: " + |
34 |
> |
System.out.println("Average time: " + |
35 |
|
((endTime - startTime) / np) + |
36 |
|
"ns"); |
37 |
|
} |
43 |
|
public void run() { |
44 |
|
final AtomicReference t = turn; |
45 |
|
final Thread other = this.other; |
46 |
< |
if (turn == null || other == null) |
46 |
> |
if (turn == null || other == null) |
47 |
|
throw new NullPointerException(); |
48 |
|
int p = 0; |
49 |
|
for (int i = 0; i < iters; ++i) { |
56 |
|
LockSupport.unpark(other); |
57 |
|
nparks = p; |
58 |
|
System.out.println("parks: " + p); |
59 |
– |
|
59 |
|
} |
60 |
|
} |
61 |
|
} |
63 |
– |
|
64 |
– |
|