先定义一个线程嘛import java.util.Date; class Mythread extends Thread { private int hours; private int minutes; private runtime=false; public void run()//实现run方法嘛 { Date date= new Date(); while(true) { try { Thread.sleep(1000); if ( (date.getHours==hours) && (date.getMinutes==minutes) && (!runtime) ) { System.out.println("我开始执行"); runtime=true; } } catch(Exception e) { System.out.println(e); }
} public Mythread(int hours,int minutes) { this.hours=hours; this.minutes=minutes; } } }public class Test { public static void main(String args[]) { new Mythread(12,15).start();//启动线程,让他在每天12点15分开始执行 } }//以上代码没有测试.不过应该没有什么问题.
对了.上面代码修改一下. 把Date date = new Date(); 这句放到while体内 然后 date.getHours() 和date.getMinutes() 这里是两个方法.要加上扩号
要是用while循环的话 会不会浪费很多cpu时间片在循环判断上呢?
要是用while循环的话 会不会浪费很多cpu时间片在循环判断上呢?
那你只写一个执行类 public class Test { public static .... { System.out.println("我正在执行"); } }然后把这个类放到windows计划任务中吧 呵呵
private final Log log = LogFactory.getLog(this.getClass());
/**
* 初始化
*/
public void contextInitialized(ServletContextEvent event) {
timer = new java.util.Timer(true);
log.info("计划任务启动");
// event.getServletContext().log("定时器已启动");
//timer.schedule(new MyTask(event.getServletContext()), 0, 1*60*1000);
//timer.schedule(new MyOtherTask(event.getServletContext()), 0, 1*60*1000);
timer.schedule(new CloseJobFair(event.getServletContext()), 0, ONE_HOUR);
timer.schedule(new SendNewEpJobInfoToJobSeeker(event.getServletContext()), 0, ONE_HOUR);
//timer.schedule(new DelIndexHtml(event.getServletContext()), 0, 30*60*1000);
// event.getServletContext().log("已经添加任务调度表");
class Mythread extends Thread
{
private int hours;
private int minutes;
private runtime=false;
public void run()//实现run方法嘛
{
Date date= new Date();
while(true)
{
try
{
Thread.sleep(1000);
if ( (date.getHours==hours) && (date.getMinutes==minutes) && (!runtime) ) {
System.out.println("我开始执行");
runtime=true;
}
}
catch(Exception e)
{
System.out.println(e);
}
}
public Mythread(int hours,int minutes)
{
this.hours=hours;
this.minutes=minutes;
}
}
}public class Test
{
public static void main(String args[])
{
new Mythread(12,15).start();//启动线程,让他在每天12点15分开始执行
}
}//以上代码没有测试.不过应该没有什么问题.
把Date date = new Date();
这句放到while体内
然后
date.getHours() 和date.getMinutes() 这里是两个方法.要加上扩号
会不会浪费很多cpu时间片在循环判断上呢?
会不会浪费很多cpu时间片在循环判断上呢?
public class Test
{
public static ....
{
System.out.println("我正在执行");
}
}然后把这个类放到windows计划任务中吧
呵呵