一、任务定时调度
使用 Timer 、 TimerTask
1、TimerTask
2、Timer
3、代码
package com.dhu.thread.others;
import java.util.Timer;
import java.util.TimerTask;
/**
* 任务定时调度:Timer和TimerTask类
* @author zhou
*
*/
public class TimerTest01 {
public static void main(String[] args) {
Timer timer = new Timer("工作狂");
timer.schedule(new MyTask(), 2000, 200); //2秒后执行,每隔200毫秒执行一次方法
}
}
class MyTask extends TimerTask{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("放空...");
}
System.out.println("end --- ");
}
}
二、任务调度框架 QUARTZ
1、下载
2、使用
(1)解压
(2)运行示例demo代码
(3)在项目中加入jar
(4)加入log4j日志文件的配置
3、代码
(1)HelloJob线程
/*
* All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*/
package com.dhu.thread.others;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
* 任务
* @author zhou
*
*/
public class HelloJob implements Job {
public HelloJob() {
}
public void execute(JobExecutionContext context)
throws JobExecutionException {
System.out.println("===============Hello World! - " + new Date() + "===============");
}
}
(2)
/*
* All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*/
package com.dhu.thread.others;
import static org.quartz.DateBuilder.evenSecondDateAfterNow;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
public class SimpleExample {
public void run() throws Exception {
Logger log = LoggerFactory.getLogger(SimpleExample.class);
//1.创建Schedule的工厂 First we must get a reference to a scheduler
SchedulerFactory sf = new StdSchedulerFactory();
//2.从工厂中获取调度器
Scheduler sched = sf.getScheduler();
//时间
Date runTime = evenSecondDateAfterNow();
//3.创建JodDetail
JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build();
//4.触发器:在下一秒调度HelloJob任务
Trigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runTime).build();
//5.注册任务和触发条件
sched.scheduleJob(job, trigger);
log.info(job.getKey() + " will run at: " + runTime);
sched.start();
// run the job!
try {
//5秒后停止
Thread.sleep(5L * 1000L);
} catch (Exception e) {
}
// shut down the scheduler
sched.shutdown(true);
}
public static void main(String[] args) throws Exception {
SimpleExample example = new SimpleExample();
example.run();
}
}
(3)运行结果
4、代码调优
(1)