TimerTask在遇到修改系统时间不能正常工作,自己写一个简单的TimerTask和Timer
来源:互联网 发布:索尼爱立信w980 软件 编辑:程序博客网 时间:2024/04/29 14:14
下面是一个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);
}
}
- TimerTask在遇到修改系统时间不能正常工作,自己写一个简单的TimerTask和Timer
- Timer和TimerTask的简单使用
- Timer 和 TimerTask的简单使用
- 简单了解Timer和TimerTask
- Timer和TimerTask的使用
- Timer和TimerTask的使用
- ICE的Timer和TimerTask
- Timer 和TimerTask的使用
- Timer和TimerTask的例子
- Java的Timer和TimerTask
- Timer和TimerTask的使用
- Timer和TimerTask的使用
- Timer和TimerTask的知识点
- Timer和TimerTask的用法
- Timer和TimerTask的使用
- Timer,TimerTask简单讲解
- java之Timer和TimerTask简单demo
- Timer和TimerTask在Android中的用法
- 现代企业信息化的设计原则
- Building A Master Image - Part 2: Preparing Windows XP
- 中国的大学生很累
- Apache与IIS共存80端口
- C语言系列--内存管理
- TimerTask在遇到修改系统时间不能正常工作,自己写一个简单的TimerTask和Timer
- 对TServerSocket实行IP绑定之二(继承方式,只用于演示,除非必要不推荐使用)
- 第一次踩点
- 新手入门:在JSP页面中的几种传参方式
- 从刘若英现象看社会“逆流”
- 网站防采集代码
- 手机短信息SMS的程序开发
- 人脉就是财脉 直销商应该如何打造个人口碑
- 图灵奖得主姚期智印象(转贴)