package com.feinno.test; public class PrintInQueue { public static void main(String[] args) { Object object= new Object(); for(int i=0; i<10; i++){ new Printer(object); } } } class Printer extends Thread{ public static int i; public static boolean runFlag = true; public static Object synFlag; public Printer(Object synFlag){ this.synFlag = synFlag; start(); } public void run(){ synchronized (synFlag) { while(runFlag){ i++; if(i%2 == 1){ try { System.out.println("before wait:"+i); synFlag.wait(50); System.out.println("after wait:"+i); } catch (InterruptedException e) { e.printStackTrace(); } }else{ System.out.println("before notify:"+i); synFlag.notifyAll(); System.out.println("after notify:"+i); try { Thread.currentThread().sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } if(i == 30){ runFlag = false; System.out.println(i); System.exit(0); } } } } }