站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2SE 专题(50分)关于timer类的使用,如何在以下API代码中加入定期执行功能?

(50分)关于timer类的使用,如何在以下API代码中加入定期执行功能?

摘要: (50分)关于timer类的使用,如何在以下API代码中加入定期执行功能?


package org.fan.Sendsms;

import java.net.*;
import java.io.*;

public class Sendnow
{
public void Sendsms(){};
public void Send(String des,String Content) throws IOException
{
Sendsms(des,Content);
}
public void Send(String [] des,String Content) throws IOException
{
for(int i=0;i-#60;des.length;i++)
{
Sendsms(des[i],Content);
}
}
public void Sendsms(String des,String Content)
throws java.io.IOException
{
try
{
URL sms = new URL(-#34;http://202.120.107.211:8800/?PhoneNumber=-#34;+des+-#34;-#38;Text=-#34;+URLEncoder.encode(Content,-#34;GB2312-#34;)+-#34;-#38;charset=GB2312-#34; );

sms.openStream( );

System.out.println(-#34;sms发送完毕-#34;);
}

catch (MalformedURLException e)
{
System.err.println(e);
}
catch (IOException e)
{
System.err.println(e);
}

}
}

需求的API是Send(String des,String Content,Date date),新加参数date实现定期执行

我的想法是用timer.schedule(Task,date,0)的方法实现,但不知道这里的以上代码定义成这里的Task

求教


Send(String des, String Content, Date date){
Timer timer = new Timer();

timer.schedule(new TimerTask(){
public void run(){
//add your code here to send your msg;
}
}, date, 0);
}

继承并重写TimerTask里的run方法,就可以用timer.schedule(...)来指定时间触发它。


to larryzhao(Larry.Zhao) :
thx
我试了下,好象有点问题,
public void run(){}因为是inner class而得不到des,Content的传值,
还有继承TimerTask时也报了异常
麻烦看看是否有误,代码和异常如下

public class Sendnow extends TimerTask
{
public void Sendsms(){};

public void Send(String des,String Content,Date date) throws IOException
{
Sendsms(des,Content,date);
}

public void Sendsms(String des,String Content,Date date)
throws java.io.IOException
{
Timer timer = new Timer();

timer.schedule(new TimerTask()
{
public void run()
{
try
{
System.out.println(des+Content+-#34;sms发送完毕-#34;);
}
catch (MalformedURLException e)
{
System.err.println(e);
}
catch (IOException e)
{
System.err.println(e);
}
}
}, date, 0);
}
}

报错:
Sendnow.java:10: Sendnow is not abstract and does not override abstract method r
un() in java.util.TimerTask
public class Sendnow extends TimerTask
^
Sendnow.java:33: local variable des is accessed from within inner class; needs t
o be declared final
System.out.println(des+Content+-#34;sms发送完毕-#34;);
^


public class Sendnow
{
public void Sendsms()
{
};

public void Send(String des, String Content, Date date) throws IOException
{
Sendsms(des, Content, date);
}

public void Sendsms(String des, String Content, Date date) throws java.io.IOException
{
Timer timer = new Timer();

final String str1= des;
final String str2= Content;
timer.schedule(new TimerTask()
{
public void run()
{
try
{
System.out.println(str1 + str2 + -#34;sms发送完毕-#34;);
}
catch (MalformedURLException e)
{
System.err.println(e);
}
catch (IOException e)
{
System.err.println(e);
}
}
}, date, 0);
}
}


改好了,只要有一个TimerTask对象就好了,你用Sendnow去继承TimerTask就出了第一个问题了,我这种写法就是写起来方便而已,对于你的项目而言,你还是自己写一个如下SendTask extends TimerTask 里面记录你要的数据比较好。

关于内部类的问题,我回答的时候的确没有考虑到你要访问的是局部变量,象下面这样改就可以使用父类的fields了

import java.io.IOException;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class Sendnow {
String theDes;
String theContent;

public void Sendsms() {
};

public void Send(String des, String Content, Date date) throws IOException {
Sendsms(des, Content, date);
}

public void Sendsms(String des, String Content, Date date)
throws java.io.IOException {

theDes = des;
theContent = Content;

Timer timer = new Timer();

timer.schedule(new TimerTask() {
public void run() {
System.out.println(Sendnow.this.theDes + Sendnow.this.theContent + -#34;sms发送完毕-#34;);
}
}, date, 0);
}
}


↑返回目录
前一篇: 小程序,根据用户输入给出不同的输出
后一篇: Fibonacci问题大家看看!