一种定时任务的写法

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ThreadTest {
    private ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);

    class scheduledTask implements Runnable {
        public void run() {
            System.out.println("I am a Scheduled task," + new Time(System.currentTimeMillis()));
        }

    public void init() {
        scheduledExecutorService.scheduleWithFixedDelay(new scheduledTask(), 0, 2L, TimeUnit.SECONDS);
    }

    }

    public static void main(String[] args) {
        ThreadTest threadTest = new ThreadTest();
        threadTest.init();
    }
}

你可能感兴趣的:(java,日常)