让你看看我的研究成果package observ;import java.io.*;
import java.util.*;/**
* 用Observable的方法 实现定时器
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author rex
* @version 1.0
*/
class BeWatchTimeing extends Observable implements Runnable{ private int seconds = 0;
private long start;
private long time;
Thread t; /**
* 构造函数
* @param seconds int
*/
public BeWatchTimeing(int seconds){
//等待的时间 秒
this.seconds = seconds;
//等待的时间 毫秒
this.time = this.seconds*1000;
t = new Thread(this);
} /**
* 开始计时
*/
public void start(){
//启动计时
//当前时间的毫秒数
this.start = System.currentTimeMillis();
t.start();
} /**
* run
*/
public void run(){ try{
while(true){
if(System.currentTimeMillis()-this.start > this.time){
setChanged();
notifyObservers(new Boolean(true));
break;
}
}
}catch(Exception e){
}
}}package observ;import java.io.*;
import java.util.*;/**
* 测试用的执行功能过程
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author rex
* @version 1.0
*/
public class Connectting implements Observer, Runnable{
int j = 0;
Thread t;
private boolean flag = false; /**
* 构造函数
*/
public Connectting(){
} /**
* 开始执行connect
*/
public void connect(){
t = new Thread(this);
t.start();
} /**
* 是否执行完
* @return boolean
*/
public boolean isAlive(){
return t.isAlive();
} /**
* 执行结果
* @return boolean
*/
public boolean isConnected(){
return this.flag;
} /**
* Thread 的 run
*/
public void run(){ try{
System.out.println("Connectting");
for (int i = 30; i>0; i--){
//System.out.print(0);
j++;
t.sleep(100);
}
flag = true;
System.out.println("Connect time is "+j+" ticks. "+flag); }catch(InterruptedException e){
flag = false;
} } /**
* 定时器的 时间到时停止执行
* @param obj Observable
* @param arg Object
*/
public void update(Observable obj, Object arg){
//中断线程
//t.interrupt();
t.stop();
System.out.println("Connect time out after "+j+" ticks.");
}
}package observ;import java.io.*;
import java.util.*;/**
* TestRun
* <p>Title: </p>
* <p>Description: 测试定时器的执行 </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author rex
* @version 1.0
*/
class TestRun{ /**
* main
* @param args String
*/
public static void main(String args[]){
boolean flag=false;
int i = 0;
try{ Connectting conn = new Connectting();
BeWatchTimeing timer = new BeWatchTimeing(1); /**
* 把conn 加入定时器, 是定时器能到时通知conn 执行update
*/
timer.addObserver(conn);
//启动定时线程
timer.start();
/**
*启动需要在一定时间内完成的过程
*/
conn.connect();
/**
*等待 需要在一定时间内完成的过程 执行完毕(正常完成或者被定时器关闭)
*/
while(conn.isAlive()){
/**
*每100 ms检测是否执行完
*/
Thread.sleep(100);
i++;
}
/**
*取得执行结果
*/
flag = conn.isConnected();
System.out.println("check flag "+i+" times. "+flag);
}catch(Exception e){
System.out.println(e.toString());
} }
}
import java.util.*;/**
* 用Observable的方法 实现定时器
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author rex
* @version 1.0
*/
class BeWatchTimeing extends Observable implements Runnable{ private int seconds = 0;
private long start;
private long time;
Thread t; /**
* 构造函数
* @param seconds int
*/
public BeWatchTimeing(int seconds){
//等待的时间 秒
this.seconds = seconds;
//等待的时间 毫秒
this.time = this.seconds*1000;
t = new Thread(this);
} /**
* 开始计时
*/
public void start(){
//启动计时
//当前时间的毫秒数
this.start = System.currentTimeMillis();
t.start();
} /**
* run
*/
public void run(){ try{
while(true){
if(System.currentTimeMillis()-this.start > this.time){
setChanged();
notifyObservers(new Boolean(true));
break;
}
}
}catch(Exception e){
}
}}package observ;import java.io.*;
import java.util.*;/**
* 测试用的执行功能过程
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author rex
* @version 1.0
*/
public class Connectting implements Observer, Runnable{
int j = 0;
Thread t;
private boolean flag = false; /**
* 构造函数
*/
public Connectting(){
} /**
* 开始执行connect
*/
public void connect(){
t = new Thread(this);
t.start();
} /**
* 是否执行完
* @return boolean
*/
public boolean isAlive(){
return t.isAlive();
} /**
* 执行结果
* @return boolean
*/
public boolean isConnected(){
return this.flag;
} /**
* Thread 的 run
*/
public void run(){ try{
System.out.println("Connectting");
for (int i = 30; i>0; i--){
//System.out.print(0);
j++;
t.sleep(100);
}
flag = true;
System.out.println("Connect time is "+j+" ticks. "+flag); }catch(InterruptedException e){
flag = false;
} } /**
* 定时器的 时间到时停止执行
* @param obj Observable
* @param arg Object
*/
public void update(Observable obj, Object arg){
//中断线程
//t.interrupt();
t.stop();
System.out.println("Connect time out after "+j+" ticks.");
}
}package observ;import java.io.*;
import java.util.*;/**
* TestRun
* <p>Title: </p>
* <p>Description: 测试定时器的执行 </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author rex
* @version 1.0
*/
class TestRun{ /**
* main
* @param args String
*/
public static void main(String args[]){
boolean flag=false;
int i = 0;
try{ Connectting conn = new Connectting();
BeWatchTimeing timer = new BeWatchTimeing(1); /**
* 把conn 加入定时器, 是定时器能到时通知conn 执行update
*/
timer.addObserver(conn);
//启动定时线程
timer.start();
/**
*启动需要在一定时间内完成的过程
*/
conn.connect();
/**
*等待 需要在一定时间内完成的过程 执行完毕(正常完成或者被定时器关闭)
*/
while(conn.isAlive()){
/**
*每100 ms检测是否执行完
*/
Thread.sleep(100);
i++;
}
/**
*取得执行结果
*/
flag = conn.isConnected();
System.out.println("check flag "+i+" times. "+flag);
}catch(Exception e){
System.out.println(e.toString());
} }
}
int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//...代码..
}
};
new Timer(delay, taskPerformer).start();
import java.util.Timer;class ReminderTest
{
Timer timer;
public ReminderTest()
{
timer = new Timer();
timer.schedule(new RemindTask(),3*1000,3*1000);
} class RemindTask extends TimerTask
{
public void run() {
System.out.println("Time's up!");
//timer.cancel(); //Terminate the timer thread
}
} public static void main(String args[])
{
System.out.println("About to schedule task.");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 21);
calendar.set(Calendar.MINUTE, 35);
calendar.set(Calendar.SECOND, 30);
Date timeRun = calendar.getTime();
System.out.println("Task scheduled.");
new ReminderTest();
System.out.println("over!");
}
}