主要就是用了synchronized ; 然后用到了wait和notifyAll的方法
package conm.crx; import static java.lang.Thread.sleep; public class _1jiaoti { public static int state=1; private static Thread t1 = new Thread(); private static Thread t2 = new Thread(); private static Thread t3 = new Thread(); private static Object lock= new Object(); public static void main(String[] args){ t1 = new Thread(() -> { for(int i=0;i<3;i++) synchronized (lock){ if(state%3!=0){ try{ lock.wait(); }catch(Exception e){ } } System.out.println(state); state++; lock.notifyAll(); } }, "t1"); t2 = new Thread(() -> { for(int i=0;i<4;i++) synchronized (lock){ if(state%3!=1){ try{ lock.wait(); }catch(Exception e){ } } System.out.println(state); state++; lock.notifyAll(); } }, "t2"); t3 = new Thread(() -> { for(int i=0;i<3;i++) synchronized (lock){ if(state%3!=2){ try{ lock.wait(); }catch(Exception e){ } } System.out.println(state); state++; lock.notifyAll(); } }, "t3"); t1.start(); t2.start(); t3.start(); } }