Run code every second by using System.currentTimeMillis()

后端 未结 5 969
滥情空心
滥情空心 2021-02-07 14:52

I am trying to run a line of code every second by using System.currentTimeMillis();.

The code:

     while(true){
           long var = System.currentTim         


        
相关标签:
5条回答
  • 2021-02-07 15:29

    You should have to use java.util.Timer and java.util.TimerTask class.

    0 讨论(0)
  • 2021-02-07 15:40

    I'd use the java executor libraries. You can create a ScheduledPool that takes a runnable and can run for any time period you want. For example

    Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(new MyRunnable(), 0, 5, TimeUnit.SECONDS);
    

    Will run the MyRunnable class every 5 seconds. MyRunnable must implement Runnable. The trouble with this is that it will (efficiently) create a new thread each time which may or may not be desirable.

    0 讨论(0)
  • 2021-02-07 15:40

    preferred way:

    ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    

    Then pass in Runnables like:

    scheduler.scheduleWithFixedDelay(myRunnable, initDelay, delay, TimeUnit.MILLISECONDS);
    

    I wouldn't use the Timer. Schedulers are built to handle problems that Timers can cause. Also, the Thread.sleep is good for a simple program that you're writing quickly for proof of concept type things but I wouldn't use it in the enterprise world.

    0 讨论(0)
  • 2021-02-07 15:46

    If you want to busy wait for the seconds to change you can use the following.

    long lastSec = 0;
    while(true){
        long sec = System.currentTimeMillis() / 1000;
        if (sec != lastSec) {
           //code to run
           lastSec = sec;
        }//If():
    }//While
    

    A more efficient approach is to sleep until the next second.

    while(true) {
        long millis = System.currentTimeMillis();
        //code to run
        Thread.sleep(1000 - millis % 1000);
    }//While
    

    An alternative is to use a ScheduledExecutorService

    ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();
    
    ses.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            // code to run
        }
    }, 0, 1, TimeUnit.SECONDS);
    
    // when finished
    ses.shutdown();
    

    The advantage of this approach is that

    • you can have a number of tasks with different periods sharing the same thread.
    • you can have non-repeating delay or asynchronous tasks.
    • you can collect the results in another thread.
    • you can shutdown the thread pool with one command.
    0 讨论(0)
  • 2021-02-07 15:46

    Using Thread.sleep(); would be perfect for your case.

     while(true)
     {
        Thread.sleep(1000); // Waiting before run.
        // Actual work goes here.
     }
    
    0 讨论(0)
提交回复
热议问题