import java.util.LinkedList; public class Main { public static void main(String[] args) { LinkedListlist = new LinkedList<>(); list.add("apple"); list.add("banana"); list.add("orange"); System.out.println(list); } }
[apple, banana, orange]
import java.util.LinkedList; public class Consumer implements Runnable { private LinkedListThis code defines a Consumer class that implements the Runnable interface. The run() method of the Consumer class repeatedly removes items from a LinkedList of integers. If the list is empty, the consumer waits until an item is added to the list by another thread using the buffer.wait() method. This example is part of the Java Concurrency library.buffer; public Consumer(LinkedList buffer) { this.buffer = buffer; } public void run() { while (true) { synchronized (buffer) { while (buffer.isEmpty()) { try { buffer.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } int item = buffer.removeFirst(); System.out.println("Consumed item: " + item); buffer.notifyAll(); } } } }