SlideShare a Scribd company logo
import java.util.concurrent.*; 
public class ConcurrentLinkedQueueExample { 
public static void main(String[] args) { 
ConcurrentLinkedQueue<String> queue = new 
ConcurrentLinkedQueue<String>(); 
Thread producer = new Thread(new Producer(queue)); 
Thread consumer = new Thread(new Consumer(queue)); 
producer.start(); 
consumer.start(); 
} 
} 
// the producer puts strings on the queue 
class Producer implements Runnable { 
ConcurrentLinkedQueue<String> queue; 
Producer(ConcurrentLinkedQueue<String> queue){ 
this.queue = queue; 
} 
public void run() { 
System.out.println("Producer Started"); 
try { 
for (int i = 1; i < 10; i++) { 
queue.add("String" + i); 
System.out.println("Added: String" + i); 
Thread.currentThread().sleep(200); 
} 
} catch (Exception ex) { 
ex.printStackTrace(); 
} 
} 
} 
// the consumer removes strings from the queue 
class Consumer implements Runnable { 
ConcurrentLinkedQueue<String> queue;
Consumer(ConcurrentLinkedQueue<String> queue){ 
this.queue = queue; 
} 
public void run() { 
String str; 
System.out.println("Consumer Started"); 
for (int x = 0; x < 10; x++) { 
while ((str = queue.poll()) != null) { 
System.out.println("Removed: " + str); 
} 
try { 
Thread.currentThread().sleep(500); 
} catch (Exception ex) { 
ex.printStackTrace(); 
} 
} 
} 
} 
OUTPUT: 
Producer Started 
Consumer Started 
Added: String1 
Removed: String1 
Added: String2 
Added: String3 
Removed: String2 
Removed: String3 
Added: String4 
Added: String5 
Removed: String4 
Removed: String5 
Added: String6 
Added: String7 
Added: String8 
Removed: String6 
Removed: String7 
Removed: String8 
Added: String9
Removed: String9

More Related Content

DOCX
Import java
TXT
KEY
XpUg Coding Dojo: KataYahtzee in Ocp way
KEY
Agile Iphone Development
PDF
Effective Modern C++ - Item 35 & 36
PPTX
作業系統
PDF
Ownership System in Rust
PDF
Protocol handler in Gecko
Import java
XpUg Coding Dojo: KataYahtzee in Ocp way
Agile Iphone Development
Effective Modern C++ - Item 35 & 36
作業系統
Ownership System in Rust
Protocol handler in Gecko

What's hot (20)

ODP
Stackless Python 101
PDF
Use C++ to Manipulate mozSettings in Gecko
PPTX
RuntimeUnitTestToolkit for Unity(English)
PDF
Understanding greenlet
KEY
Gevent what's the point
PDF
Appengine ja-night-sapporo#1 bt
PDF
Introduction to asyncio
PPTX
разработка серверов и серверных приложений лекция №4
PPT
شرح مقرر البرمجة 2 لغة جافا - الوحدة السابعة
PPTX
Behind modern concurrency primitives
PDF
The Ring programming language version 1.10 book - Part 35 of 212
PPTX
Akka.NET streams and reactive streams
PDF
The Ring programming language version 1.9 book - Part 92 of 210
PDF
The Ring programming language version 1.7 book - Part 30 of 196
PDF
Spark Day 2017- Spark 의 과거, 현재, 미래
PDF
The Ring programming language version 1.8 book - Part 88 of 202
DOCX
Incrementos
PDF
Kotlinのcoroutine、async/awaitと同じでしょ?って思ってたけど意外と洗練されててすごいなぁって思った話をさせてほしい
PPTX
Java practice programs for beginners
Stackless Python 101
Use C++ to Manipulate mozSettings in Gecko
RuntimeUnitTestToolkit for Unity(English)
Understanding greenlet
Gevent what's the point
Appengine ja-night-sapporo#1 bt
Introduction to asyncio
разработка серверов и серверных приложений лекция №4
شرح مقرر البرمجة 2 لغة جافا - الوحدة السابعة
Behind modern concurrency primitives
The Ring programming language version 1.10 book - Part 35 of 212
Akka.NET streams and reactive streams
The Ring programming language version 1.9 book - Part 92 of 210
The Ring programming language version 1.7 book - Part 30 of 196
Spark Day 2017- Spark 의 과거, 현재, 미래
The Ring programming language version 1.8 book - Part 88 of 202
Incrementos
Kotlinのcoroutine、async/awaitと同じでしょ?って思ってたけど意外と洗練されててすごいなぁって思った話をさせてほしい
Java practice programs for beginners
Ad

Viewers also liked (12)

PPTX
βασιλικη μουγκου A.M 1020797 ετοσ 1
PPTX
Hausterre eguna
PPTX
PPTX
Library visit
PPTX
PDF
Compliance for Real-Time communications-June2016
DOC
Bilbao metroa
PPTX
Nerea europa
DOCX
PPT
Direct & indirect presentation
PPT
Acceleration analysis-review
βασιλικη μουγκου A.M 1020797 ετοσ 1
Hausterre eguna
Library visit
Compliance for Real-Time communications-June2016
Bilbao metroa
Nerea europa
Direct & indirect presentation
Acceleration analysis-review
Ad

Similar to 7th lab (20)

PDF
Description (Part A) In this lab you will write a Queue implementati.pdf
PPT
Thread
PDF
JJUG CCC 2011 Spring
PDF
DCN Practical
PDF
54240326 copy
PDF
54240326 (1)
DOCX
PQTimer.java A simple driver program to run timing t.docx
ODP
Java Concurrency
PPTX
Java Programs
DOCX
Java programs
PDF
Confitura 2012 Bad Tests, Good Tests
PDF
Prompt a user to enter a series of integers separated by spaces and .pdf
PDF
GeeCON 2012 Bad Tests, Good Tests
PPT
Taking User Input in Java
DOCX
Creating Interface- Practice Program 6.docx
PPTX
Jdk 7 4-forkjoin
DOCX
PDF
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
PDF
AJUG April 2011 Cascading example
PDF
Server1
Description (Part A) In this lab you will write a Queue implementati.pdf
Thread
JJUG CCC 2011 Spring
DCN Practical
54240326 copy
54240326 (1)
PQTimer.java A simple driver program to run timing t.docx
Java Concurrency
Java Programs
Java programs
Confitura 2012 Bad Tests, Good Tests
Prompt a user to enter a series of integers separated by spaces and .pdf
GeeCON 2012 Bad Tests, Good Tests
Taking User Input in Java
Creating Interface- Practice Program 6.docx
Jdk 7 4-forkjoin
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
AJUG April 2011 Cascading example
Server1

Recently uploaded (20)

PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
Current and future trends in Computer Vision.pptx
PPTX
Information Storage and Retrieval Techniques Unit III
PDF
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
PPTX
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
PDF
Analyzing Impact of Pakistan Economic Corridor on Import and Export in Pakist...
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
PDF
UNIT no 1 INTRODUCTION TO DBMS NOTES.pdf
PDF
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
PDF
Soil Improvement Techniques Note - Rabbi
PDF
Categorization of Factors Affecting Classification Algorithms Selection
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PPTX
communication and presentation skills 01
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Integrating Fractal Dimension and Time Series Analysis for Optimized Hyperspe...
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PPT
A5_DistSysCh1.ppt_INTRODUCTION TO DISTRIBUTED SYSTEMS
Exploratory_Data_Analysis_Fundamentals.pdf
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Current and future trends in Computer Vision.pptx
Information Storage and Retrieval Techniques Unit III
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
Analyzing Impact of Pakistan Economic Corridor on Import and Export in Pakist...
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
UNIT no 1 INTRODUCTION TO DBMS NOTES.pdf
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
Soil Improvement Techniques Note - Rabbi
Categorization of Factors Affecting Classification Algorithms Selection
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
communication and presentation skills 01
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Integrating Fractal Dimension and Time Series Analysis for Optimized Hyperspe...
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
A5_DistSysCh1.ppt_INTRODUCTION TO DISTRIBUTED SYSTEMS

7th lab

  • 1. import java.util.concurrent.*; public class ConcurrentLinkedQueueExample { public static void main(String[] args) { ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>(); Thread producer = new Thread(new Producer(queue)); Thread consumer = new Thread(new Consumer(queue)); producer.start(); consumer.start(); } } // the producer puts strings on the queue class Producer implements Runnable { ConcurrentLinkedQueue<String> queue; Producer(ConcurrentLinkedQueue<String> queue){ this.queue = queue; } public void run() { System.out.println("Producer Started"); try { for (int i = 1; i < 10; i++) { queue.add("String" + i); System.out.println("Added: String" + i); Thread.currentThread().sleep(200); } } catch (Exception ex) { ex.printStackTrace(); } } } // the consumer removes strings from the queue class Consumer implements Runnable { ConcurrentLinkedQueue<String> queue;
  • 2. Consumer(ConcurrentLinkedQueue<String> queue){ this.queue = queue; } public void run() { String str; System.out.println("Consumer Started"); for (int x = 0; x < 10; x++) { while ((str = queue.poll()) != null) { System.out.println("Removed: " + str); } try { Thread.currentThread().sleep(500); } catch (Exception ex) { ex.printStackTrace(); } } } } OUTPUT: Producer Started Consumer Started Added: String1 Removed: String1 Added: String2 Added: String3 Removed: String2 Removed: String3 Added: String4 Added: String5 Removed: String4 Removed: String5 Added: String6 Added: String7 Added: String8 Removed: String6 Removed: String7 Removed: String8 Added: String9