Skip to content

Instantly share code, notes, and snippets.

@vituchon
Last active May 27, 2020 16:39
Show Gist options
  • Save vituchon/8da108189dee47e52332 to your computer and use it in GitHub Desktop.
Save vituchon/8da108189dee47e52332 to your computer and use it in GitHub Desktop.
Fibonacci Procuder, Prime Producer and StdOut Consumer Example
import java.util.concurrent.BlockingQueue;
public class Consumer implements Runnable {
private final BlockingQueue<Long> queue;
private volatile boolean finish;
public Consumer(BlockingQueue<Long> queue) {
this.queue = queue;
this.finish = false;
}
@Override
public void run() {
while (!finish) {
try {
long number = queue.take();
System.out.println("Consumer takes " + number);
} catch (InterruptedException e) {
finish = true;
Thread.interrupted();
}
}
}
}
import java.util.concurrent.BlockingQueue;
public class FibonacciProducer implements Runnable{
private final FibonacciSequence sequence;
private final BlockingQueue<Long> queue;
private final long max;
public FibonacciProducer(BlockingQueue<Long> queue,long max) {
this.queue = queue;
this.max = max;
this.sequence = new FibonacciSequence();
}
@Override
public void run() {
long number = 0;
number = sequence.next();
while (number < max) {
try {
System.out.println("Fibonacci Produces places " + number);
queue.put(number);
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
number = sequence.next();
}
}
}
public class FibonacciSequence {
private int step;
public FibonacciSequence() {
this.step = 0;
}
public long next () {
long ret = f(step);
step++;
return ret;
}
private long f(int i) {
if (i == 0 || i == 1){
return i;
}
else {
return f(i-2) + f(i-1);
}
}
}
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class Main {
public static void main (String args[]) throws InterruptedException {
BlockingQueue<Long> queue = new ArrayBlockingQueue<>(5);
FibonacciProducer fibonacciProducer = new FibonacciProducer(queue, 50);
PrimeProducer primeProducer = new PrimeProducer(queue, 50);
Consumer consumer = new Consumer(queue);
Thread fibonacciProducerThread = new Thread(fibonacciProducer);
Thread primerProducerThread = new Thread(primeProducer);
Thread consumerThread = new Thread(consumer);
fibonacciProducerThread.start(); // comienza generador fibo
primerProducerThread.start(); // comiennza generador primos
consumerThread.start(); // comienza consumidor
fibonacciProducerThread.join(); // espero que termine generador fibo
primerProducerThread.join(); // espero que termine generador primos
consumerThread.interrupt(); // interrumpo consumidor (así puedo terminar el thread y el programa)
}
}
import java.util.concurrent.BlockingQueue;
public class PrimeProducer implements Runnable{
private final int max;
private final BlockingQueue<Long> queue;
private final PrimeSequence sequence;
public PrimeProducer(BlockingQueue<Long> queue,int max) {
this.queue = queue;
this.max = max;
this.sequence = new PrimeSequence();
}
@Override
public void run() {
long number = 0;
number = sequence.next();
while (number < max) {
try {
System.out.println("Prime Produces places " + number);
queue.put(number);
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
number = sequence.next();
}
}
}
import java.math.BigInteger;
public class PrimeSequence {
private BigInteger prime = BigInteger.ONE;
public long next () {
long res = prime.longValue();
prime = prime.nextProbablePrime();
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment