4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
– |
import java.util.Random; |
7 |
|
import java.util.concurrent.ArrayBlockingQueue; |
8 |
|
import java.util.concurrent.BlockingQueue; |
9 |
|
import java.util.concurrent.CyclicBarrier; |
18 |
|
|
19 |
|
public class ProducerConsumerLoops { |
20 |
|
static final int NCPUS = Runtime.getRuntime().availableProcessors(); |
22 |
– |
static final Random rng = new Random(); |
21 |
|
static final ExecutorService pool = Executors.newCachedThreadPool(); |
22 |
|
static boolean print = false; |
23 |
|
static int producerSum; |
243 |
|
static final class LTQasSQ<T> extends LinkedTransferQueue<T> { |
244 |
|
LTQasSQ() { super(); } |
245 |
|
public void put(T x) { |
246 |
< |
try { super.transfer(x); |
247 |
< |
} catch (InterruptedException ex) { throw new Error(); } |
246 |
> |
try { super.transfer(x); } |
247 |
> |
catch (InterruptedException ex) { throw new Error(ex); } |
248 |
|
} |
249 |
|
} |
250 |
|
|
255 |
|
if ((++calls & 1) == 0) |
256 |
|
super.put(x); |
257 |
|
else { |
258 |
< |
try { super.transfer(x); |
259 |
< |
} catch (InterruptedException ex) { |
262 |
< |
throw new Error(); |
263 |
< |
} |
258 |
> |
try { super.transfer(x); } |
259 |
> |
catch (InterruptedException ex) { throw new Error(ex); } |
260 |
|
} |
261 |
|
} |
262 |
|
} |