Package net.jcip.examples

Source Code of net.jcip.examples.PrimeProducer

package net.jcip.examples;

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

/**
* PrimeProducer
* <p/>
* Using interruption for cancellation
*
* @author Brian Goetz and Tim Peierls
*/
public class PrimeProducer extends Thread {
    private final BlockingQueue<BigInteger> queue;

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

    public void run() {
        try {
            BigInteger p = BigInteger.ONE;
            while (!Thread.currentThread().isInterrupted())
                queue.put(p = p.nextProbablePrime());
        } catch (InterruptedException consumed) {
            /* Allow thread to exit */
        }
    }

    public void cancel() {
        interrupt();
    }
}
TOP

Related Classes of net.jcip.examples.PrimeProducer

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.