forked from CodeOpsTech/ConcurrentJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueueExample.java
More file actions
26 lines (24 loc) · 973 Bytes
/
Copy pathPriorityQueueExample.java
File metadata and controls
26 lines (24 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Copyright: Ganesh Samarthyam, 2016; www.codeops.tech
import java.util.*;
// Simple PriorityQueue example. Here, we create two threads in which one thread inserts an element,
// and another thread removes an element from the priority queue.
class PriorityQueueExample {
public static void main(String []args) {
final PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
// spawn a thread that removes an element from the priority queue
new Thread() {
public void run() {
// Use remove() method in PriorityQueue to remove the element if available
System.out.println("The removed element is: " + priorityQueue.remove());
}
}.start();
// spawn a thread that inserts an element into the priority queue
new Thread() {
public void run() {
// insert Integer value 10 as an entry into the priority queue
priorityQueue.add(10);
System.out.println("Successfully added an element to the queue ");
}
}.start();
}
}