Source Code : Using the ConcurrentLinkedDeque class safely with multiple threads
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
Using the ConcurrentLinkedDeque class safely with multiple threads
import java.util.concurrent.ConcurrentLinkedDeque;
public class Test {
public static ConcurrentLinkedDeque<Item> deque = new ConcurrentLinkedDeque<>();
public static void main(String[] args) {
Thread producerThread = new Thread(new ItemProducer());
Thread consumerThread = new Thread(new ItemConsumer());
producerThread.start();
consumerThread.start();
}
}
class Item {
private String description;
private int itemId;
public String getDescription() {
return description;
}
public int getItemId() {
return itemId;
}
public Item() {
this.description = "Default Item";
this.itemId = 0;
}
public Item(String description, int itemId) {
this.description = description;
this.itemId = itemId;
}
}
class ItemProducer implements Runnable {
@Override
public void run() {
String itemName = "";
int itemId = 0;
try {
for (int i = 1; i < 8; i++) {
itemName = "Item" + i;
itemId = i;
Test.deque.add(new Item(itemName, itemId));
System.out.println("New Item Added:" + itemName + " " + itemId);
Thread.currentThread().sleep(250);
}
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
class ItemConsumer implements Runnable {
@Override
public void run() {
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
Item item;
while ((item = Test.deque.pollFirst()) != null) {
if (item == null) {
} else {
generateOrder(item);
}
}
}
private void generateOrder(Item item) {
System.out.println(item.getDescription());
System.out.println(item.getItemId());
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
Thank with us