Java 定时动作优化

Java 定时动作优化,java,Java,我想为系统的每个事务创建一个程序任务,以预留15分钟的等待时间。如果超过15分钟,程序将更改状态/状态。如果状态在15分钟内更改,则结束任务。还有更好的代码可以应用吗?如等待/睡眠功能,是否有副作用 Date myTime = new Date(); Calendar cal = Calendar.getInstance(); cal.setTime(myTime); cal.add(Calendar.MINUTE, 15);

我想为系统的每个事务创建一个程序任务,以预留15分钟的等待时间。如果超过15分钟,程序将更改状态/状态。如果状态在15分钟内更改,则结束任务。还有更好的代码可以应用吗?如等待/睡眠功能,是否有副作用

     Date myTime       = new Date();
     Calendar cal      = Calendar.getInstance();

     cal.setTime(myTime);
     cal.add(Calendar.MINUTE, 15);

     Date endTime      = cal.getTime();
     Date startTime    = new Date();

 do {


     startTime = new Date() ;

     if(checkStatus(_ID) == true )
     {
         System.out.println("Closing task");
         con.close();  // end the task
         System.exit(0); 
     }

 }while (endTime.after(startTime)) ;

// if over 15minutes, code goes here

这可能不是您正在寻找的确切解决方案,但它可能会让您了解如何使用wait/notify方法

public class Runner {
    static Object monitor = new Object();

    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MINUTE, 15);
        Date endTime = cal.getTime();

        long fifteenMinutes = 900000;
        monitor.wait(fifteenMinutes); // waits to be notified, max 15 mins

        if (new Date().after(endTime)) {
            // time ran out without a status change
        }

        System.out.println("Closing task");
        con.close();  // end the task
        System.exit(0); 
    }

    public void updateStatus() {
        // some status logic here
        monitor.notify(); // wakes up the monitor that is waiting
    }
}

这可能不是您正在寻找的确切解决方案,但它可能会让您了解如何使用wait/notify方法

public class Runner {
    static Object monitor = new Object();

    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MINUTE, 15);
        Date endTime = cal.getTime();

        long fifteenMinutes = 900000;
        monitor.wait(fifteenMinutes); // waits to be notified, max 15 mins

        if (new Date().after(endTime)) {
            // time ran out without a status change
        }

        System.out.println("Closing task");
        con.close();  // end the task
        System.exit(0); 
    }

    public void updateStatus() {
        // some status logic here
        monitor.notify(); // wakes up the monitor that is waiting
    }
}

我看到的唯一问题是,您的代码正在进行一种繁忙的等待。程序的线程一直忙于循环

您可以为每个循环添加一些时间间隔,这些时间间隔将放弃cpu

添加Thread.sleep(1000)在循环中等待一秒钟

sleep(1000)将在1000毫秒内放弃cpu,因此您可以:

do {


     startTime = new Date() ;

     if(checkStatus(_ID) == true )
     {
         System.out.println("Closing task");
         con.close();  // end the task
         System.exit(0); 
     }
     Thread.sleep(1000); //give up cpu for the following 1 second and 'do nothing' in this time

 }while (endTime.after(startTime)) ;

我看到的唯一问题是,您的代码正在进行一种繁忙的等待。程序的线程一直忙于循环

您可以为每个循环添加一些时间间隔,这些时间间隔将放弃cpu

添加Thread.sleep(1000)在循环中等待一秒钟

sleep(1000)将在1000毫秒内放弃cpu,因此您可以:

do {


     startTime = new Date() ;

     if(checkStatus(_ID) == true )
     {
         System.out.println("Closing task");
         con.close();  // end the task
         System.exit(0); 
     }
     Thread.sleep(1000); //give up cpu for the following 1 second and 'do nothing' in this time

 }while (endTime.after(startTime)) ;

事实上,我也意识到我的代码是繁忙的,因为它做循环,也检查状态。这就是我在这里发帖寻求建议的原因。我不熟悉Sleep/Wait函数,因此如果使用Thread.Sleep函数,我想寻求更好的解决方案。例如,当我打开数据库连接时,每个循环睡眠1分钟。连接会失败并连接吗?让连接忙这么长时间可能会有问题,因此您可能希望在每次需要时都能获得连接,而不仅仅是一次,并在连接使用之间等待一分钟。但是大多数数据库连接管理器都支持某种类型的保持活动。您通常可以在数据库的连接字符串中配置它。事实上,我也意识到我的代码正在忙碌,因为它正在执行循环并检查状态。这就是我在这里发帖寻求建议的原因。我不熟悉Sleep/Wait函数,因此如果使用Thread.Sleep函数,我想寻求更好的解决方案。例如,当我打开数据库连接时,每个循环睡眠1分钟。连接会失败并连接吗?让连接忙这么长时间可能会有问题,因此您可能希望在每次需要时都能获得连接,而不仅仅是一次,并在连接使用之间等待一分钟。但是大多数数据库连接管理器都支持某种类型的保持活动。您通常可以在数据库的连接字符串中配置它。我正在打开数据库连接以获取状态。这是否意味着,我将在15分钟后知道结果?其他一些线程将不得不调用
updateStatus()
方法。在这种情况下,一旦调用notify(),等待的线程将立即“唤醒”。我正在打开数据库连接以获取状态。这是否意味着,我将在15分钟后知道结果?其他一些线程将不得不调用
updateStatus()
方法。在这种情况下,一旦调用notify(),等待的线程将立即“唤醒”。