Executors之ScheduledThreadExecutor

ScheduledThreadPoolExecutor 继承自ThreadPoolExecutor实现了ScheduledExecutorService接口。主要完成定时或者周期的执行线程任务。


111.png

执行过程:


22.png

demo:
package thread;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * Created by Torres on 17/3/16.
 */
public class MyThread implements Runnable {

    private String name;
    public MyThread(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    @Override
    public void run() {
        System.out.println(getName() + "true start:" + nowTime());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(getName() + "true end:" + nowTime());

    }
    public static String nowTime() {
        SimpleDateFormat dataFormate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dataFormate.format(new Date());
    }
}
package thread;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.Test;

/**
 * Created by Torres on 17/3/16.
 */
public class ScheduledThreadPoolTest {
    @Test
    public void testScheduleThread() {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);

        System.out.println("main开始时间" + MyThread.nowTime());
        for (int i = 0; i < 3; i++) {
            MyThread myThread = new MyThread("thread" + i);
            System.out.println("thread" + i + "开始时间" + MyThread.nowTime());
            scheduledExecutorService.schedule(myThread, 5, TimeUnit.SECONDS);//延时5s 执行
            //scheduledExecutorService.scheduleWithFixedDelay(myThread, 5, 2, TimeUnit.SECONDS);
        }
        try {
            Thread.sleep(70000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        scheduledExecutorService.shutdown();//7s 后关闭不再接受执行线程
        while (!scheduledExecutorService.isTerminated()) {//all thread等待结束
        }
        System.out.println("main结束时间" + MyThread.nowTime());
    }
}

(1) scheduledExecutorService.schedule(myThread, 5, TimeUnit.SECONDS)
结果:

main开始时间2017-03-16 22:18:04
thread0开始时间2017-03-16 22:18:04
thread1开始时间2017-03-16 22:18:04
thread2开始时间2017-03-16 22:18:04
thread1true start:2017-03-16 22:18:09
thread0true start:2017-03-16 22:18:09
thread2true start:2017-03-16 22:18:09
thread1true end:2017-03-16 22:18:12
thread0true end:2017-03-16 22:18:12
thread2true end:2017-03-16 22:18:12
main结束时间2017-03-16 22:19:14

可以看出主线程启动子线程:scheduledExecutorService.schedule(myThread, 5, TimeUnit.SECONDS)后5S开始thread0,1,2开始执行。 scheduledExecutorService.shutdown();//7s 后关闭不再接受执行线程。shutdown()表示scheduledExecutorService关闭不再接受新的线程执行(如果线程已经开始,则等待此线程结束)这里如果没有 Thread.sleep(70000);则所有线程不会执行。while (!scheduledExecutorService.isTerminated()) {//all thread等待结束 这里是主线程等待所有的子线程结束
}

 public ScheduledFuture<?> schedule(Runnable command,
                                       long delay, TimeUnit unit);
启动线程后延时delay 个unit单位执行command 任务。只有一次

(2)scheduledExecutorService.scheduleWithFixedDelay(myThread, 5, 2, TimeUnit.SECONDS);
结果:

main开始时间2017-03-16 22:26:39
thread0开始时间2017-03-16 22:26:39
thread1开始时间2017-03-16 22:26:39
thread2开始时间2017-03-16 22:26:39
thread1true start:2017-03-16 22:26:44
thread0true start:2017-03-16 22:26:44
thread2true start:2017-03-16 22:26:44
thread1true end:2017-03-16 22:26:47
thread0true end:2017-03-16 22:26:47
thread2true end:2017-03-16 22:26:47
thread1true start:2017-03-16 22:26:49
thread0true start:2017-03-16 22:26:49
thread2true start:2017-03-16 22:26:49
thread2true end:2017-03-16 22:26:52
thread1true end:2017-03-16 22:26:52
thread0true end:2017-03-16 22:26:52
thread2true start:2017-03-16 22:26:54
thread1true start:2017-03-16 22:26:54
thread0true start:2017-03-16 22:26:54
thread0true end:2017-03-16 22:26:57
thread1true end:2017-03-16 22:26:57
thread2true end:2017-03-16 22:26:57
thread1true start:2017-03-16 22:26:59
thread2true start:2017-03-16 22:26:59
thread0true start:2017-03-16 22:26:59

可以看出线程开始延时5后首次执行,以后每隔2S周期执行任务。

    public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
                                                     long initialDelay,
                                                     long delay,
                                                     TimeUnit unit);
//初始的延迟initialDelay 结束后周期性地执行给定任务command,在一次调用完成和下一次调用开始之间有长度为delay的延迟。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容