Executors.newScheduledThreadPool

public ScheduledFuture scheduleWithFixedDelay(Runnable command,
                                                 long initialDelay,
                                                 long delay,
                                                 TimeUnit unit);
package day05.exce;

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

class Temp extends Thread{
    public void run(){
        System.out.println("run");
    }
}

public class ScheduledJob {

    public static void main(String[] a){
        Temp command = new Temp();
        ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(1);

        //command  第一次执行延迟时间(5s后开始执行) 1(每隔1s执行) 后面是时间单位
        ScheduledFuture scheduleTask =  scheduled.scheduleWithFixedDelay(command, 5, 1, TimeUnit.SECONDS);

    }

}

 

你可能感兴趣的:(多线程,多线程)