下面是一个Timer:
public class MyTimer extends Thread ... {
private static Vector<MyTimerTaskInterface> mttVector=new Vector<MyTimerTaskInterface>();
private static boolean updata = false;
public void run()
...{
while (true)
...{
try
...{
sleep(1000);
} catch (InterruptedException e)
...{
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (mttVector)
...{
try
...{
while (updata || mttVector.isEmpty())
mttVector.wait();
} catch (InterruptedException e)
...{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Iterator<MyTimerTaskInterface> it = mttVector.iterator();
while (it.hasNext())
...{
MyTimerTaskInterface m = it.next();
if (m.runCondition())m.doTask();
}
}
}
public static final void logout(MyTimerTaskInterface mtt)
...{
synchronized (mttVector)
...{
for (int i = 0; i < mttVector.size(); i++)
...{
MyTimerTaskInterface t = mttVector.get(i);
if (mtt == t)
mttVector.remove(i);
}
System.out.println("mttVector.size()="+mttVector.size());
mttVector.notifyAll();
}
}
public static final void login( MyTimerTaskInterface mtt)...{
synchronized (mttVector)
...{
for (int i = 0; i < mttVector.size(); i++)
...{
MyTimerTaskInterface t = mttVector.get(i);
if (mtt == t)...{System.out.println("不要反复调用该方法!");return;}
}
mttVector.add(mtt);
mttVector.notifyAll();
}
}
}
下面是接口:
public
interface
MyTimerTaskInterface
...
{ public void doTask(); public void login(MyTimer mt); public void logout(MyTimer mt); public boolean runCondition(); public void setInterval(long interval); public void setSchedule(long time); }
public
interface
MyTimerTaskInterface
...
{ public void doTask(); public void login(MyTimer mt); public void logout(MyTimer mt); public boolean runCondition(); public void setInterval(long interval); public void setSchedule(long time); }
下面是测试:
public
class
Test
implements
MyTimerTaskInterface
...
{
String t="t";
public Test()...{
}
public Test(String t)...{
this.t=t;
}
public void doTask()...{
String s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
System.out.println(t+": now Time is:"+s);
}
public void login(MyTimer mt)...{
mt.login(this);
}
public void logout(MyTimer mt)...{
mt.logout(this);
}
public void setInterval(long interval)...{}
public void setSchedule(long time)...{}
public boolean runCondition()...{
String s = new SimpleDateFormat("ss").format(new Date());
if(s.equals("00"))return true;
return false;
}
public static void main(String args[])...{
Test t=new Test();
MyTimer mt=new MyTimer();
mt.start();
t.login(mt);
Test t2=new Test("t2");
try
...{
Thread.sleep(30000);
} catch (InterruptedException e)
...{
e.printStackTrace();
}
t2.login(mt);
try
...{
Thread.sleep(30000);
} catch (InterruptedException e)
...{
e.printStackTrace();
}
System.out.println("logout(t)");
mt.logout(t);
}
}
String t="t";
public Test()...{
}
public Test(String t)...{
this.t=t;
}
public void doTask()...{
String s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
System.out.println(t+": now Time is:"+s);
}
public void login(MyTimer mt)...{
mt.login(this);
}
public void logout(MyTimer mt)...{
mt.logout(this);
}
public void setInterval(long interval)...{}
public void setSchedule(long time)...{}
public boolean runCondition()...{
String s = new SimpleDateFormat("ss").format(new Date());
if(s.equals("00"))return true;
return false;
}
public static void main(String args[])...{
Test t=new Test();
MyTimer mt=new MyTimer();
mt.start();
t.login(mt);
Test t2=new Test("t2");
try
...{
Thread.sleep(30000);
} catch (InterruptedException e)
...{
e.printStackTrace();
}
t2.login(mt);
try
...{
Thread.sleep(30000);
} catch (InterruptedException e)
...{
e.printStackTrace();
}
System.out.println("logout(t)");
mt.logout(t);
}
}