import java.util.LinkedList; public class LinkedListDemo { public static void main(String[] args) { LinkedListnames = new LinkedList<>(); names.add("Alice"); names.add("Bob"); names.add("Charlie"); // Notify waiting threads that an element has been added names.notify(); } }
import java.util.LinkedList; public class LinkedListDemo { public static void main(String[] args) { LinkedListThis code creates a LinkedList of integers and adds three numbers to it. The main thread then waits for an element to be added or removed from the list using the wait() method. Once the notify() method is called on the list, the main thread wakes up and prints the first element of the list. The java.util LinkedList and notify() methods are part of the Java standard library.numbers = new LinkedList<>(); numbers.add(1); numbers.add(2); numbers.add(3); // Wait for an element to be added or removed synchronized (numbers) { try { numbers.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } // Print the first element of the list System.out.println(numbers.getFirst()); } }