81,094
社区成员
发帖
与我相关
我的任务
分享
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);
}
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);
}
}
}
}
}