This is a basic but often asked interview question. Use two classic interview questions to close the topic. Thread collaboration in Java is typically implemented in Wait/Notify mode.

One. Two threads alternately print odd and even numbers from 0 to 100

* 1. Wait and notify are methods of the Object class. * 2. Wait and notify must be executed in synchronized code blocks; otherwise, exceptions will be thrown. */ public class WaitNotifyPrint { private static int count = 0; Private static final Object lock = new Object(); Public static void main(String[] args) throws InterruptedException {new Thread(new TurningRunner(), "even ").start(); New Thread(new TurningRunner(), "odd ").start(); } //1. //2. After printing, wake up other threads and go to sleep. static class TurningRunner implements Runnable { @Override public void run() { while (count <= 100) { synchronized Println (thread.currentthread ().getname () + ":" + count++); // After printing, wake up other threads lock.notify(); If (count <= 100) {try {// Sleep lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } } } }Copy the code

Note that alternating print odd and even can also be synchronized, interested in their own baidu ~

Two. To achieve the classic producer – consumer model

/* * Scenario description: An EventStorage is used to simulate the warehouse. Producer represents Producer, Consumer represents Consumer, and Producer and Consumer collaborate on the warehouse. * There are plenty of comments below to explain, ~ */ public class ProducerConsumerModel {public static void main(String[] args) {// Initialize the EventStorage EventStorage  = new EventStorage(); // Initialize Producer Producer = new Producer(eventStorage); Consumer = new Consumer(eventStorage); // Start new Thread(producer).start(); new Thread(consumer).start(); } } class Producer implements Runnable { private EventStorage storage; public Producer( EventStorage storage) { this.storage = storage; } @override public void run() {for (int I = 0; i < 100; i++) { storage.put(); } } } class Consumer implements Runnable { private EventStorage storage; public Consumer( EventStorage storage) { this.storage = storage; } @override public void run() {for (int I = 0; i < 100; i++) { storage.take(); } } } class EventStorage { private int maxSize; Private LinkedList<Date> storage; // Notice that the constructor defines the repository size as 10 and uses LinkedList to store objects public EventStorage() {maxSize = 10; storage = new LinkedList<>(); } public synchronized void put() {while (storage.size() == maxSize) {try {wait(); } catch (InterruptedException e) { e.printStackTrace(); Storage.add (new Date()); System.out.println(" storage "+ storage.size() +" ); notify(); } public synchronized void take() {while (storage.size() == 0) {try {wait(); } catch (InterruptedException e) { e.printStackTrace(); Poll.out.println (" get "+ storage.poll() +" + storage.size())); notify(); }}Copy the code

3. To summarize

Wait/Notify is a lower-level implementation that is currently packaged with JDK tools or frameworks such as blocking queues and thread collaboration tools. But these tools are always the same, and understanding the essence is essential.