相关疑难解决方法(0)

如何在Java中安排定期任务?

我需要安排一个任务以固定的时间间隔运行.如何在长时间间隔(例如每8小时)的支持下完成此操作?

我正在使用java.util.Timer.scheduleAtFixedRate.是否java.util.Timer.scheduleAtFixedRate支持长时间间隔?

java scheduled-tasks

168
推荐指数
10
解决办法
24万
查看次数

如何使用ScheduledExecutorService在特定时间每天运行某些任务?

我每天凌晨5点都在尝试完成某项任务.所以我决定使用ScheduledExecutorService这个,但到目前为止,我已经看到了一些示例,显示了如何每隔几分钟运行一次任务.

而且我无法找到任何显示如何在早上的特定时间(早上5点)每天运行任务的例子,同时也考虑夏令时的事实 -

以下是我的代码,每15分钟运行一次 -

public class ScheduledTaskExample {
    private final ScheduledExecutorService scheduler = Executors
        .newScheduledThreadPool(1);

    public void startScheduleTask() {
    /**
    * not using the taskHandle returned here, but it can be used to cancel
    * the task, or check if it's done (for recurring tasks, that's not
    * going to be very useful)
    */
    final ScheduledFuture<?> taskHandle = scheduler.scheduleAtFixedRate(
        new Runnable() {
            public void run() {
                try {
                    getDataFromDatabase();
                }catch(Exception ex) {
                    ex.printStackTrace(); //or loggger would be …
Run Code Online (Sandbox Code Playgroud)

java background-thread timertask scheduledexecutorservice

65
推荐指数
5
解决办法
11万
查看次数

如何安排任务运行一次?

我想延迟做一些事情,就像设置倒计时器一样,在一段时间后"做一件事".

我希望我的其余程序在我等待的时候继续运行,所以我尝试自己Thread创建一个包含一分钟延迟的程序:

public class Scratch {
    private static boolean outOfTime = false;

    public static void main(String[] args) {
        Thread countdown = new Thread() {
            @Override
            public void run() {
                try {
                    // wait a while
                    System.out.println("Starting one-minute countdown now...");
                    Thread.sleep(60 * 1000);

                    // do the thing
                    outOfTime = true;
                    System.out.println("Out of time!");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        countdown.start();

        while (!outOfTime) {
            try {
                Thread.sleep(1000);
                System.out.println("do other stuff here");
            } catch (InterruptedException e) {
                e.printStackTrace(); …
Run Code Online (Sandbox Code Playgroud)

java scheduled-tasks scheduledexecutorservice

22
推荐指数
1
解决办法
2万
查看次数

如何在java中的某个时间启动进程?

我有一个应用程序从用户获取开始和结束时间并启动特定进程(开始时间)运行到(结束时间),对于示例我使用TimerTask实用程序,以防它只从当前时间开始进程和运行到(结束时间)我无法设置开始时间如何在java中共同设置用户时间(开始时间)和系统时间

//my sample  program
import java.sql.Date;
import java.util.Timer;
import java.util.TimerTask;

public class Main {
  public static void main(String[] argv) throws Exception {

     int numberOfMillisecondsInTheFuture=1000;

//    start time= dynamically set by user
//    end time =dynamically set by user


    Date timeToRun = new Date(System.currentTimeMillis() + numberOfMillisecondsInTheFuture);//here is the problem.
    Timer timer = new Timer();

    timer.schedule(new TimerTask() {
      public void run() {

      //System.out.println("doing");
      //doing some task not related to this question
      }
    }, timeToRun);
  }
Run Code Online (Sandbox Code Playgroud)

java timer scheduled-tasks timertask

5
推荐指数
0
解决办法
4988
查看次数

无限的“while 循环”会消耗过多的资源吗

我想在特定时间触发 Java 代码,比如说每天凌晨 1 点。

我写了一个线程如下:

while (true) {
    if (currentTime = 1AM) {
        doSomething;
    }       
}
Run Code Online (Sandbox Code Playgroud)

我有点担心,while loop继续运行,会不会减慢机器速度,或者吃掉处理资源?

我的第一个问题是,我在想如果我每秒只循环 1 次,会更好吗?

while (true) {
    if (currentTime = 1AM) {
        doSomething;
    }       

    Thread.sleep(1000);
}
Run Code Online (Sandbox Code Playgroud)

我的第二个问题是,有时我看到while loop如下写,大部分时间是获取 Java 锁,如果我们在while loop下面这样写,谁能解释一下有多昂贵(对不起,如果这是非常基本的问题)?

while (isLock) {
    // do nothing
}

doThisIfNoLock();
Run Code Online (Sandbox Code Playgroud)

我扩展上面的想法,如果我创建一个空线程,并且while loop内部无限空,那么线程实际消耗了多少资源(处理能力)?因为循环里面没有内容,按照我的想象,循环会跑的非常快,最终会占用很多个CPU周期?真的吗?

java loops infinite-loop while-loop

5
推荐指数
1
解决办法
3347
查看次数