想大致了解多线程的相关知识的可以先看看这篇文章。
Java核心技术点之多线程
线程各个状态的变化。
看下wait
import java.util.concurrent.ArrayBlockingQueue; /** * 生产者-消费者 * * @author zzs . * @since 2018/3/24 */ public class BlockingQueueTest { private int size = 20; private ArrayBlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<Integer>(size); public static void main(String[] args) { BlockingQueueTest test = new BlockingQueueTest(); Producer producer = test.new Producer(); Consumer consumer = test.new Consumer(); producer.start(); consumer.start(); } class Consumer extends Thread { @Override public void run() { while (true) { try { //从阻塞队列中取出一个元素 blockingQueue.take(); System.out.println("队列剩余" + blockingQueue.size() + "个元素"); } catch (InterruptedException e) { } } } } class Producer extends Thread { @Override public void run() { while (true) { try { //向阻塞队列中插入一个元素 blockingQueue.put(1); System.out.println("队列剩余空间:" + (size - blockingQueue.size())); } catch (InterruptedException e) { } } } } }