日期:2014-05-20 浏览次数:20680 次
import java.text.SimpleDateFormat; import java.util.Date; public class C { private static int i = 0; synchronized static void increase() { if (i < 9) { i++; } else { C.class.notify(); } } public static void main(String[] args) { synchronized (C.class) { long begin = System.currentTimeMillis(); for (int i = 0; i <= 9; i++) { new Thread(new T()).start(); } try { C.class.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(new SimpleDateFormat("ss秒SSS毫秒") .format(new Date(System.currentTimeMillis() - begin))); } } static class T implements Runnable { @Override public void run() { // dosomething C.increase(); } } }