Created
March 31, 2021 13:31
-
-
Save AnyTimeTraveler/a19b0599700804eee14e9e4c3e17c8f2 to your computer and use it in GitHub Desktop.
ConcurrentLinkedQueue Example in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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