首先来个简单那的实例:
我们以一个实例思考,车站,每辆车只能停靠10分钟就必须出站.传统方式,我们是以车站为单位,通过多线程来控制每辆车的出站情况!
输出:
the bus station hava 0 cars !
Fri May 21 14:53:58 CST 2010 : a car[car01] into the bus station !
Bus [car01] :: in station !
the bus station hava 1 cars !
Fri May 21 14:54:02 CST 2010 : a car[car02] into the bus station !
Bus [car01] :: in station !
Bus [car02] :: in station !
the bus station hava 2 cars !
Fri May 21 14:54:08 CST 2010 : a car[car01] left the bus station !
Fri May 21 14:54:08 CST 2010 : a car[car03] into the bus station !
Bus [car02] :: in station !
Bus [car03] :: in station !
the bus station hava 2 cars !
Fri May 21 14:54:12 CST 2010 : a car[car02] left the bus station !
Bus [car03] :: in station !
the bus station hava 1 cars !
Fri May 21 14:54:18 CST 2010 : a car[car03] left the bus station !
现在我们可以使用Java Timer来从每辆车自身来处理!
打印:
the bus station hava 0 cars !
Fri May 21 14:57:11 CST 2010 : a car[car01] into the bus station !
Bus [car01] :: in station !
the bus station hava 1 cars !
Fri May 21 14:57:15 CST 2010 : a car[car02] into the bus station !
Bus [car01] :: in station !
Bus [car02] :: in station !
the bus station hava 2 cars !
Fri May 21 14:57:21 CST 2010 : a car[car01] left the bus station !
Fri May 21 14:57:21 CST 2010 : a car[car03] into the bus station !
Bus [car02] :: in station !
Bus [car03] :: in station !
the bus station hava 2 cars !
Fri May 21 14:57:25 CST 2010 : a car[car02] left the bus station !
Bus [car03] :: in station !
the bus station hava 1 cars !
Fri May 21 14:57:31 CST 2010 : a car[car03] left the bus station !
但仔细想想,其实这2种方式,是完全一致的!!!仅供参考!