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

7th lab

  • 1.
    import java.util.concurrent.*; publicclass 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
  • 3.