Java 每5分钟更新一个变量的值

Java 每5分钟更新一个变量的值,java,multithreading,parallel-processing,Java,Multithreading,Parallel Processing,我有一个变量,它的值需要根据条件每5分钟更新一次。我知道我需要开始一个不同的线程。但是我该怎么做呢?如何使用java.util.Timer?如何使用java.util.Timer 安排一项任务要容易得多 对于java来说,在预定的时间内运行程序是一个很好的选择 Quartz调度程序的功能是将任务推迟到给定时间运行 循环执行也可用 使用 安排一项任务要容易得多 对于java来说,在预定的时间内运行程序是一个很好的选择 Quartz调度程序的功能是将任务推迟到给定时间运行 循环执行也可用 使用

我有一个变量,它的值需要根据条件每5分钟更新一次。我知道我需要开始一个不同的线程。但是我该怎么做呢?

如何使用
java.util.Timer

如何使用
java.util.Timer

安排一项任务要容易得多

对于
java
来说,在预定的时间内运行程序是一个很好的选择

  • Quartz调度程序的功能是将任务推迟到给定时间运行
  • 循环执行也可用
使用

安排一项任务要容易得多

对于
java
来说,在预定的时间内运行程序是一个很好的选择

  • Quartz调度程序的功能是将任务推迟到给定时间运行
  • 循环执行也可用
使用

使用


线程执行此操作的方法,

Thread t= new Thread(new Runnable() 
    {           
        public void run() 
        {
            while(true)
            {
                try 
                {
                    Thread.sleep(5 * 60 * 1000);
                    //change your variable values as required                       
                } 
                catch (InterruptedException e) 
                {

                }                                   
            }
        }
    });
public static void main(String args[]) 
    {
        ScheduledThreadPoolExecutor myPool = new ScheduledThreadPoolExecutor(2);    
        myPool.scheduleAtFixedRate(new MyJob(), 0, 5 , TimeUnit.MINUTES);       
    }
    class MyJob implements Runnable
    {
        public void run() 
        {
            //change your variable values as required in this function as this will be invoked every 5 minutes             
        }
    }
线程池执行器方式,

Thread t= new Thread(new Runnable() 
    {           
        public void run() 
        {
            while(true)
            {
                try 
                {
                    Thread.sleep(5 * 60 * 1000);
                    //change your variable values as required                       
                } 
                catch (InterruptedException e) 
                {

                }                                   
            }
        }
    });
public static void main(String args[]) 
    {
        ScheduledThreadPoolExecutor myPool = new ScheduledThreadPoolExecutor(2);    
        myPool.scheduleAtFixedRate(new MyJob(), 0, 5 , TimeUnit.MINUTES);       
    }
    class MyJob implements Runnable
    {
        public void run() 
        {
            //change your variable values as required in this function as this will be invoked every 5 minutes             
        }
    }
按计时器执行

Timer timer =new Timer();
        TimerTask task = new TimerTask() 
        {

            @Override
            public void run() 
            {
                //change your variable values as required in this function as this will be invoked every 5 minutes   

            }
        };
        timer.schedule(task, new Date(), 5 * 60 * 1000); //Use this if you want the new task to be invoked after completion of prior task only              
        timer.scheduleAtFixedRate(task, new Date(), 5 * 60 * 1000);//Use this if you want the new task to be invoked after fixed interval 
                                                                   //but will no depend or wait on completion of the prior task 

线程执行此操作的方法,

Thread t= new Thread(new Runnable() 
    {           
        public void run() 
        {
            while(true)
            {
                try 
                {
                    Thread.sleep(5 * 60 * 1000);
                    //change your variable values as required                       
                } 
                catch (InterruptedException e) 
                {

                }                                   
            }
        }
    });
public static void main(String args[]) 
    {
        ScheduledThreadPoolExecutor myPool = new ScheduledThreadPoolExecutor(2);    
        myPool.scheduleAtFixedRate(new MyJob(), 0, 5 , TimeUnit.MINUTES);       
    }
    class MyJob implements Runnable
    {
        public void run() 
        {
            //change your variable values as required in this function as this will be invoked every 5 minutes             
        }
    }
线程池执行器方式,

Thread t= new Thread(new Runnable() 
    {           
        public void run() 
        {
            while(true)
            {
                try 
                {
                    Thread.sleep(5 * 60 * 1000);
                    //change your variable values as required                       
                } 
                catch (InterruptedException e) 
                {

                }                                   
            }
        }
    });
public static void main(String args[]) 
    {
        ScheduledThreadPoolExecutor myPool = new ScheduledThreadPoolExecutor(2);    
        myPool.scheduleAtFixedRate(new MyJob(), 0, 5 , TimeUnit.MINUTES);       
    }
    class MyJob implements Runnable
    {
        public void run() 
        {
            //change your variable values as required in this function as this will be invoked every 5 minutes             
        }
    }
按计时器执行

Timer timer =new Timer();
        TimerTask task = new TimerTask() 
        {

            @Override
            public void run() 
            {
                //change your variable values as required in this function as this will be invoked every 5 minutes   

            }
        };
        timer.schedule(task, new Date(), 5 * 60 * 1000); //Use this if you want the new task to be invoked after completion of prior task only              
        timer.scheduleAtFixedRate(task, new Date(), 5 * 60 * 1000);//Use this if you want the new task to be invoked after fixed interval 
                                                                   //but will no depend or wait on completion of the prior task 

如果您使用的是Spring,则可以使用@Scheduled注释您的方法,例如:

// Invoke method every 300000 ms (5 minutes)
@Scheduled(fixedDelay=300000)
public void myMethod() {
    // Your code here
}

或者您可以查看Spring的文档:

如果您使用的是Spring,您可以使用@Scheduled注释您的方法,例如:

// Invoke method every 300000 ms (5 minutes)
@Scheduled(fixedDelay=300000)
public void myMethod() {
    // Your code here
}

或者你可以看看Spring的文档:

Google“ScheduledThreadPoolExecutor”当它被标记时,你真的不必在标题中写
Java
。。。。看看这个:你做了什么研究?你试过什么代码?谷歌“ScheduledThreadPoolExecutor”你真的不必在标题中写
Java
,当它被标记时。。。。看看这个:你做了什么研究?你试过什么代码?你的研究助理也是这样。你说的“老办法”是指“由于各种原因而经常被打破的办法,从来都不是一个好主意”,我想是吧?@Voo不,老办法并不意味着做事情不是一个好主意,这是一种更有趣的方式,可以说您的旧方式实现至少有一个错误处理中断异常的showstopper错误,并且存在时钟漂移。是的,这两件事意味着这不是一个好的做事方式。@Voo谢谢你的评论,我删除了旧的方式来回答你的观点,考虑到“旧的方式”你的意思是“由于几个原因总是被打破的方式,从来不是一个好主意”,我想?@Voo不,旧的方式并不意味着做事情不是一个好主意,这是一种更有趣的方式,可以说您的旧方式实现至少有一个错误处理中断异常的showstopper错误,并且存在时钟漂移。是的,这两件事意味着这不是一个好的做事方式。@Voo谢谢你的评论,我删除了旧的方式来回答你的观点