java 有什么方法让指定的任务在每天指定的时间段内反复执行吗?
答案:1 悬赏:50 手机版
解决时间 2021-03-28 03:47
- 提问者网友:你挡着我发光了
- 2021-03-27 05:56
java 有什么方法让指定的任务在每天指定的时间段内反复执行吗?
最佳答案
- 五星知识达人网友:蓝房子
- 2021-03-27 06:29
package thread;
import java.util.*;
import org.apache.log4j.Logger;
public class Thread_timing extends Thread{
static Logger logger = Logger.getLogger(Thread_timing.class);
// 构造函数为私有类型,只能在函数内部定义
private Thread_timing() {
}
// 用单一模式,只能创建一个对象
private static Thread_timing Thread_timing = new Thread_timing();
// 定义一个静态对象,用来取类的唯一对象,
public static Thread_timing getThread_timing() {
// 获取线程的状态
String state = Thread_timing.getState().toString();
System.out.println("定时线程状态:"+state);
if (state.equals("NEW") || state.equals("TERMINATED")) {
Thread_timing = new Thread_timing();
}
return Thread_timing;
}
UpdateCityUsering ucu = new UpdateCityUsering();
UpdateUserAddressing uua = new UpdateUserAddressing();
XiaFaTiming xft = new XiaFaTiming();
liShiHuiZong ls = new liShiHuiZong();
public void run(){
System.out.println("定时线程已启动");
while(true){
Calendar date = Calendar.getInstance();
int hour = date.get(Calendar.HOUR_OF_DAY);
//当到达8点
while(hour ==8){
//添加业务逻辑
//等待1小时
try {
Thread.sleep(3600);
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
try {
Thread.sleep(60000);//每隔一分钟检测一次,看看时间到了没
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String []arg){
Thread_timing thr=new Thread_timing();
thr.start();
}
}
import java.util.*;
import org.apache.log4j.Logger;
public class Thread_timing extends Thread{
static Logger logger = Logger.getLogger(Thread_timing.class);
// 构造函数为私有类型,只能在函数内部定义
private Thread_timing() {
}
// 用单一模式,只能创建一个对象
private static Thread_timing Thread_timing = new Thread_timing();
// 定义一个静态对象,用来取类的唯一对象,
public static Thread_timing getThread_timing() {
// 获取线程的状态
String state = Thread_timing.getState().toString();
System.out.println("定时线程状态:"+state);
if (state.equals("NEW") || state.equals("TERMINATED")) {
Thread_timing = new Thread_timing();
}
return Thread_timing;
}
UpdateCityUsering ucu = new UpdateCityUsering();
UpdateUserAddressing uua = new UpdateUserAddressing();
XiaFaTiming xft = new XiaFaTiming();
liShiHuiZong ls = new liShiHuiZong();
public void run(){
System.out.println("定时线程已启动");
while(true){
Calendar date = Calendar.getInstance();
int hour = date.get(Calendar.HOUR_OF_DAY);
//当到达8点
while(hour ==8){
//添加业务逻辑
//等待1小时
try {
Thread.sleep(3600);
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
try {
Thread.sleep(60000);//每隔一分钟检测一次,看看时间到了没
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String []arg){
Thread_timing thr=new Thread_timing();
thr.start();
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯