JAVA定时器可以传参吗?

灵魂的追宿 2010-11-16 03:24:53
我想用Timer和TimerTask做一个定时器,请问可以传参吗

package org.tiling.scheduling.examples;
import java.util.Timer;
import java.util.TimerTask;
public class EggTimer {
private final Timer timer = new Timer();
private final int minutes;
public EggTimer(int minutes) {
this.minutes = minutes;
}
public void start() {
timer.schedule(new TimerTask() {
public void run() {
playSound();
timer.cancel();
}
private void playSound() {
System.out.println("Your egg is ready!");//想把参数传到这来
// Start a new thread to play a sound...
}
}, minutes * 60 * 1000);
}
public static void main(String[] args) {
EggTimer eggTimer = new EggTimer(2);
eggTimer.start();
}
}

我能把页面的参数传到playSound()方法中吗?
...全文
776 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
liguangwen86 2010-11-16
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 jypapgl 的回复:]
quartz
[/Quote]
表较好
zcy9979420 2010-11-16
  • 打赏
  • 举报
回复
过来学习了
gukuitian 2010-11-16
  • 打赏
  • 举报
回复
方法是你自己写的,想加参就加,有什么问题么?
灵魂的追宿 2010-11-16
  • 打赏
  • 举报
回复
LS的是什么东西,用spring?我们公司不用spring,所以只能用java自带的。。。。
jypapgl 2010-11-16
  • 打赏
  • 举报
回复
quartz
灵魂的追宿 2010-11-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 gouxiongyaya 的回复:]

Java code

public void start(final String str) {
timer.schedule(new TimerTask() {
public void run() {
playSound();
timer.cancel();
}……
[/Quote]
是不是多个参数也可以?只要是final类型的就可以了?我这个方法能处理并发吗?
灵魂的追宿 2010-11-16
  • 打赏
  • 举报
回复
不太懂你写的,我要的效果是这样的

package sdcss.client.dyxxwh;
import java.util.Timer;
import java.util.TimerTask;

import dfsf.core.DataSet;
public class EggTimer {
private final Timer timer = new Timer();
private final int minutes;
public EggTimer(int minutes) {
this.minutes = minutes;
}
public void start() {
timer.schedule(new TimerTask() {
public void run() {
playSound();
timer.cancel();
}

private void playSound() {
String sql = "";//想把sql这个参数传过来,在调用start方法的时候
DataSet ds = new DataSet();
try {
ds.getSqlResult(sql);
} catch (Exception e) {
e.printStackTrace();
}
}
}, minutes * 60 * 1000);
}
public static void main(String[] args) {
EggTimer eggTimer = new EggTimer(2);
eggTimer.start();
}
}
gouxiongyaya 2010-11-16
  • 打赏
  • 举报
回复

public void start(final String str) {
timer.schedule(new TimerTask() {
public void run() {
playSound();
timer.cancel();
}
private void playSound() {
System.out.println(str);//想把参数传到这来
// Start a new thread to play a sound...
}
}, minutes* 1000);
}
public static void main(String[] args) {
EggTimer eggTimer = new EggTimer(2);
eggTimer.start("123");
}
liujian6918123 2010-11-16
  • 打赏
  • 举报
回复
package com.im.controler;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import com.im.context.IMContext;
import com.im.dto.Visitor;

public class VisitorTimeOutControler {
private static Timer timer=new Timer();
private static long delay;
private static TimerTask timerTask =new TimerTask() {
@Override
public void run() {
execute();
}
};

/**
* 超时检查
*/
private static void execute() {
for (String key : IMContext.getVisitorKeys()) {
Visitor visitor = IMContext.getVisitor(key);
Date endDate = null;
try {
endDate = IMContext.dateFormat.parse(visitor.getEndTime() == null
|| visitor.getEndTime().equals("") ? visitor
.getStartTime() : visitor.getEndTime());
} catch (ParseException e) {
e.printStackTrace();
}

Calendar calendar = Calendar.getInstance();
calendar.setTime(endDate);
long offset = System.currentTimeMillis()
- calendar.getTimeInMillis();
if (offset > 0.5 * 60 * 1000) {
// 等待超时
System.out.println("移除过期对话!!--->:" + visitor.getVid());
IMContext.removeVisitor(visitor.getIp());
continue;
} else {
System.out.println(offset);
}
}
}

/**
* 启动超时检查
* @param delay
*/
public static void start(long delay){
VisitorTimeOutControler.setDelay(delay);
timer.schedule(timerTask, VisitorTimeOutControler.delay);
System.out.println("Visitor超时检查启动...");
}

/**
* 返回延期时间
* @return long
*/
public static long getDelay() {
return VisitorTimeOutControler.delay;
}

/**
* 设置延期时间
* @param delay
*/
public static void setDelay(long delay) {
VisitorTimeOutControler.delay = delay;
}
}
灵魂的追宿 2010-11-16
  • 打赏
  • 举报
回复
没人帮忙解决吗?

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧