public class Print implements Runnable { private char c; private Print objNext; private boolean isSetup = false; public Print(char c) { this.c = c; } public Print(char c, Print objNext) { super(); this.c = c; this.objNext = objNext; } public boolean isSetup() { return isSetup; } public void setSetup(boolean isSetup) { this.isSetup = isSetup; } public char getC() { return c; } public void setC(char c) { this.c = c; } public Object getObjNext() { return objNext; } public void setObjNext(Print objNext) { this.objNext = objNext; } @Override public void run() { try { synchronized (this) { isSetup = true; System.out.println(c + "等待"); if(c!='a') { this.notify(); this.wait(); } } } catch (InterruptedException e1) { e1.printStackTrace(); } for (int i = 0; i < 10; i++) { try { synchronized (this) { System.out.println("进入" + c); synchronized (objNext) { System.out.println(c + " " + (i + 1)); if(!objNext.isSetup()) { System.out.println("等待线程" +objNext.getC() +"启动"); objNext.wait(); } objNext.notify(); System.out.println("通知" + objNext.getC()); } System.out.println(getC() + "等待"); if (i < 9) this.wait(); } } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(getC() + "退出"); } public static void main(String[] args) throws InterruptedException { Print a = new Print('a'); Print b = new Print('b'); Print c = new Print('c'); Print d = new Print('d'); a.setObjNext(b); b.setObjNext(c); c.setObjNext(d); d.setObjNext(a); new Thread(a).start(); new Thread(b).start(); new Thread(c).start(); new Thread(d).start(); } }