请教!java定时器TimerTask 如何立即停止定时 不用再运行一个周期

鲤鱼爱上猫 2019-07-12 09:49:48
this.cancel();只能在下一个周期才能停止,我想当我想要停止的时候立即停止
比如当我输入 结束1 的时候 立即停止 启动1 的定时,不会在1分钟后停止

public static void main(String[] args) throws InterruptedException {

while(true){
System.out.print("输入");
Scanner scan = new Scanner(System.in);
String read = scan.nextLine();
System.out.println("输入数据:"+read);

if(read.equals("启动1")){
new MailSend().triger("2019-06-11 16:05:00","sssssssssss",5,60 * 1000);
}else if(read.equals("启动2")){
new MailSend().triger("2019-06-11 09:30:00","ddddddddddd",6,180 * 1000);
}else if(read.equals("结束1")){
new MailSend().setA(5);
}else if(read.equals("结束2")){
new MailSend().setA(6);
}
}

}




public void triger(String date1,String s,int j,long tses) throws InterruptedException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
try {
date = sdf.parse(date1);
} catch (ParseException e) {
e.printStackTrace();
}
TimerTask task = new TimerTask() {
@Override
public void run() {
if(a == j){
this.cancel();
System.out.println("结束任务");
return;
}
System.out.println(sdf.format(new Date()) + "-------" + s);

}
};
Timer timer = new Timer();
long delay = 0;
long intevalPeriod = 1000;
timer.scheduleAtFixedRate(task, date, tses);

}

...全文
543 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
maradona1984 2019-07-12
  • 打赏
  • 举报
回复
你代码这些当然做不到立即停止,因为你停止的代码依赖于任务的执行 你应该在setA方法里调用TimerTask 的cancel()方法 你的代码需要改造很多地方,而且setA方法修改的是静态变量?这代码写的挺奇怪的,并不是个好习惯 稍微改造了下你的代码

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;

public class MailSend {

	private Map<Integer, TimerTask> map = new HashMap<>();
	private Timer timer = new Timer();
	public void triger(String s, int j, long tses) throws InterruptedException, ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		TimerTask task = new TimerTask() {
			@Override
			public void run() {
				System.out.println(sdf.format(new Date()) + "-------" + s);
			}
		};
		timer.scheduleAtFixedRate(task, 1000L, tses);
		map.put(j, task);
	}
	public void stop(int sign) {
		TimerTask task = map.get(sign);
		if (task != null) {
			task.cancel();
			System.out.printf("结束任务[%s]", sign);
			map.remove(sign);
		}
	}
	public static void main(String[] args) throws Exception {
		MailSend mailSend = new MailSend();
		try (Scanner scan = new Scanner(System.in)) {
			while (true) {
				System.out.print("输入");
				String read = scan.nextLine();
				System.out.println("输入数据:" + read);
				if (read.equals("启动1")) {
					mailSend.triger("sssssssssss", 5, 10 * 1000);
				} else if (read.equals("启动2")) {
					mailSend.triger("ddddddddddd", 6, 15 * 1000);
				} else if (read.equals("结束1")) {
					mailSend.stop(5);
				} else if (read.equals("结束2")) {
					mailSend.stop(5);
				}
			}
		}
	}

}
鲤鱼爱上猫 2019-07-12
  • 打赏
  • 举报
回复
引用 1 楼 maradona1984 的回复:
你代码这些当然做不到立即停止,因为你停止的代码依赖于任务的执行
你应该在setA方法里调用TimerTask 的cancel()方法

你的代码需要改造很多地方,而且setA方法修改的是静态变量?这代码写的挺奇怪的,并不是个好习惯
稍微改造了下你的代码

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;

public class MailSend {

private Map<Integer, TimerTask> map = new HashMap<>();
private Timer timer = new Timer();
public void triger(String s, int j, long tses) throws InterruptedException, ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TimerTask task = new TimerTask() {
@Override
public void run() {
System.out.println(sdf.format(new Date()) + "-------" + s);
}
};
timer.scheduleAtFixedRate(task, 1000L, tses);
map.put(j, task);
}
public void stop(int sign) {
TimerTask task = map.get(sign);
if (task != null) {
task.cancel();
System.out.printf("结束任务[%s]", sign);
map.remove(sign);
}
}
public static void main(String[] args) throws Exception {
MailSend mailSend = new MailSend();
try (Scanner scan = new Scanner(System.in)) {
while (true) {
System.out.print("输入");
String read = scan.nextLine();
System.out.println("输入数据:" + read);
if (read.equals("启动1")) {
mailSend.triger("sssssssssss", 5, 10 * 1000);
} else if (read.equals("启动2")) {
mailSend.triger("ddddddddddd", 6, 15 * 1000);
} else if (read.equals("结束1")) {
mailSend.stop(5);
} else if (read.equals("结束2")) {
mailSend.stop(5);
}
}
}
}

}
谢谢!! 谢谢!!

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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