Skip to content

Instantly share code, notes, and snippets.

@coldcue
Created October 21, 2013 18:41
Show Gist options
  • Save coldcue/7088759 to your computer and use it in GitHub Desktop.
Save coldcue/7088759 to your computer and use it in GitHub Desktop.
package com.producer;
import java.util.Random;
public class Application {
public static void main(String[] args) {
FIFOBuffer<String> fifoBuffer = new FIFOBuffer<>(10);
Consumer<String> stringConsumer;
Random random = new Random();
for (int i = 0; i < 3; i++) {
stringConsumer = new Consumer<>(fifoBuffer, "sc" + i, random.nextInt(5000) + 100);
stringConsumer.start();
}
Thread elso = new Thread(new Producer("elso", fifoBuffer));
elso.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread masodik = new Thread(new Producer("masodik", fifoBuffer));
masodik.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread harmadik = new Thread(new Producer("harmadik", fifoBuffer));
harmadik.start();
}
}
package com.producer;
/**
* Created with IntelliJ IDEA.
* User: Andrew
* Date: 10/15/13
* Time: 5:08 PM
*/
public class Consumer<T> extends Thread {
private FIFOBuffer<T> fifoBuffer;
private String text;
private int sleep;
public Consumer(FIFOBuffer<T> fifoBuffer, String text, int sleep) {
this.fifoBuffer = fifoBuffer;
this.text = text;
this.sleep = sleep;
}
@Override
public void run() {
//noinspection InfiniteLoopStatement
while (true) {
try {
System.out.println("consumed " + text + " " + fifoBuffer.get());
Thread.sleep(sleep);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
package com.producer;
import java.util.ArrayList;
import java.util.List;
/**
* Created with IntelliJ IDEA.
* User: Andrew
* Date: 10/15/13
*/
public class FIFOBuffer<T> {
private List<T> list;
private int size;
public FIFOBuffer(int size) {
this.size = size;
list = new ArrayList<>(size);
}
public synchronized void put(T object) throws Exception {
while (list.size() >= size)
this.wait();
list.add(object);
this.notify();
System.out.println("put " + Thread.currentThread().getName());
}
public synchronized T get() throws Exception {
while (list.size() == 0)
this.wait();
T object = list.get(list.size() - 1);
list.remove(object);
this.notify();
System.out.println("get " + Thread.currentThread().getName());
return object;
}
}
package com.producer;
/**
* Created with IntelliJ IDEA.
* User: Andrew
* Date: 10/15/13
* Time: 4:05 PM
*/
public class Producer implements Runnable {
private String text;
private int n = 0;
private FIFOBuffer<String> fifoBuffer;
public Producer(String text, FIFOBuffer<String> fifoBuffer) {
this.fifoBuffer = fifoBuffer;
this.text = text;
}
@Override
public void run() {
try {
go();
} catch (Exception e) {
e.printStackTrace();
}
}
public void go() throws Exception {
//noinspection InfiniteLoopStatement
while (true) {
String time = Long.toString(System.currentTimeMillis());
String text = this.text + " " + n + " " + time.substring(time.length() - 6);
fifoBuffer.put(text);
System.out.println(text);
n++;
Thread.sleep(1000);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment