add a newer timer and two files to test it
[mfw.git] / demo / test / Timer_Test.java
blob4cd8e96057cd326ebfced199a5b0288696ea669e
1 /**
2 * Copyright 2011 Huize Dai <geb.1989@gmail.com>
4 * This file is part of MFW.
6 * MFW is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * MFW is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with MFW. If not, see <http://www.gnu.org/licenses/>.
19 package test;
21 import mfw.pool.Mission;
22 import mfw.timer.newer.*;
24 public class Timer_Test {
25 public static ClockThreadGroup clock = new ClockThreadGroup(3, true);
26 public static final Object lock = new Object();
28 public static void main(String[] args) throws Throwable {
29 for (int i = 1; i < 10; i++) {
30 new MissionImpl(i).start();
32 Thread.sleep(1000);
33 synchronized (lock) {
34 lock.notifyAll();
36 Thread.sleep(5000);
37 clock.close();
40 static class MissionImpl extends Mission {
41 private int id;
43 public MissionImpl(int id) {
44 this.id = id;
47 public void run() {
48 synchronized (lock) {
49 try {
50 lock.wait();
51 } catch (InterruptedException e) {
52 e.printStackTrace();
53 return;
56 for (int i = 1; i < 10; i++) {
57 clock.add(new TimerEventImpl(id, i), 10 * i);
62 static class TimerEventImpl extends TimerEvent {
64 private int id;
65 private int num;
66 private int time = 0;
67 private long previous = System.currentTimeMillis();
68 private int[] log = new int[5];
70 public TimerEventImpl(int id, int num) {
71 this.id = id;
72 this.num = num;
75 public boolean isContinue() {
76 long now;
77 System.out.println(now = System.currentTimeMillis());
78 log[time++] = (int) (now - previous);
79 previous = now;
80 if (time >= 5) {
81 System.out.println("-------" + id + " " + num + " " + time
82 + ":" + log[0] + "," + log[1] + "," + log[2] + ","
83 + log[3] + "," + log[4]);
85 return time < 5;