Package net.jcip.examples

Source Code of net.jcip.examples.BrokenPrimeProducer

package net.jcip.examples;

import java.math.BigInteger;
import java.util.concurrent.*;

/**
* BrokenPrimeProducer
* <p/>
* Unreliable cancellation that can leave producers stuck in a blocking operation
*
* @author Brian Goetz and Tim Peierls
*/
class BrokenPrimeProducer extends Thread {
    private final BlockingQueue<BigInteger> queue;
    private volatile boolean cancelled = false;

    BrokenPrimeProducer(BlockingQueue<BigInteger> queue) {
        this.queue = queue;
    }

    public void run() {
        try {
            BigInteger p = BigInteger.ONE;
            while (!cancelled)
                queue.put(p = p.nextProbablePrime());
        } catch (InterruptedException consumed) {
        }
    }

    public void cancel() {
        cancelled = true;
    }
}
TOP

Related Classes of net.jcip.examples.BrokenPrimeProducer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.