import java.util.*; import java.io.*; public class Main { static LinkedList<Object> list=new LinkedList<>(); final Object lock = new Object(); static int max; public void put(Object obj) { synchronized(lock) { if(list.size()>=max) { try { lock.wait(); } catch(Exception e) { e.printStackTrace(); } } list.add(obj); lock.notify(); } } public Object take() { synchronized(lock) { if(list.size()<=0) { try { lock.wait(); } catch(Exception e) { e.printStackTrace(); } } lock.notify(); return list.pop(); } } public static void main(String[] args) { max=5; Main m=new Main(); Thread t1=new Thread( new Runnable() { public void run() { m.put("a"); m.put("b"); m.put("c"); m.put("d"); m.put("e"); m.put("f"); } }); t1.start(); try { Thread.sleep(1000); System.out.println("当前容器的大小:"+list.size()); } catch(Exception e) { e.printStackTrace(); } Thread t2=new Thread( new Runnable() { public void run() { Object o = m.take(); Object j = m.take(); System.out.println(o+""+j); } }); t2.start(); try { Thread.sleep(10); System.out.println(list.size()); } catch(Exception e) { e.printStackTrace(); System.out.println("当前容器的大 小:"+list.size()); } } }