Skip to content

Instantly share code, notes, and snippets.

@AnyTimeTraveler
Created March 31, 2021 13:31
Show Gist options
  • Save AnyTimeTraveler/a19b0599700804eee14e9e4c3e17c8f2 to your computer and use it in GitHub Desktop.
Save AnyTimeTraveler/a19b0599700804eee14e9e4c3e17c8f2 to your computer and use it in GitHub Desktop.
ConcurrentLinkedQueue Example in Java
import java.util.concurrent.ConcurrentLinkedQueue;
public class Queues {
private static final ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<>();
private static boolean running = true;
public static void main(String[] args) throws InterruptedException {
new Thread(Queues::produce).start();
new Thread(Queues::consume).start();
Thread.sleep(100);
running = false;
}
private static void produce() {
for (int i = 0; running; i++) {
queue.offer(i);
}
}
private static void consume() {
while (running) {
System.out.println("Received: " + queue.poll());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment