Skip to content

Instantly share code, notes, and snippets.

@gurbuzali
Created February 10, 2014 11:39
Show Gist options
  • Save gurbuzali/8914396 to your computer and use it in GitHub Desktop.
Save gurbuzali/8914396 to your computer and use it in GitHub Desktop.
public class QueueTest {
public static void main(String[] args) throws InterruptedException {
final HazelcastInstance instance = Hazelcast.newHazelcastInstance();
Thread c = new Thread(new Consumer(instance));
Thread p = new Thread(new Producer(instance));
c.start();
Thread.sleep(5000);
p.start();
}
static class Consumer implements Runnable {
final HazelcastInstance instance;
Consumer(HazelcastInstance instance) {
this.instance = instance;
}
public void run() {
final IQueue queue = instance.getQueue("queue");
while (!Thread.currentThread().isInterrupted()) {
try {
final Object taken = queue.take();
System.err.println("consumed item: " + taken);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
static class Producer implements Runnable {
final HazelcastInstance instance;
Producer(HazelcastInstance instance) {
this.instance = instance;
}
public void run() {
final IQueue queue = instance.getQueue("queue");
int counter = 0;
while (!Thread.currentThread().isInterrupted()) {
String item = "item" + counter++;
final boolean offered = queue.offer(item);
System.err.println("produced item: " + item + ", offered: " + offered);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment