多线程学习(3):ScheduledExecutorService(newScheduledThreadPool) 与 Runnable接口实现

本文介绍如何使用Java创建ScheduledExecutorService线程池,并通过示例演示如何调度任务延迟执行及周期性执行。文章展示了ScheduledThreadPoolExecutor的具体配置方法,并提供了一个完整的测试案例。

2018年7月9日14:13:06


【1】

使用参考:



【2】线程池

    /**
     * Creates a thread pool that can schedule commands to run after a
     * given delay, or to execute periodically.
     * @param corePoolSize the number of threads to keep in the pool,
     * even if they are idle.
     * @return a newly created scheduled thread pool
     * @throws IllegalArgumentException if {@code corePoolSize < 0}
     */
    public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
        return new ScheduledThreadPoolExecutor(corePoolSize);
    }


【3】测试例子

WorkerThread.java:(实现Runnable接口的任务类)

package test01.SheduledThreadPoolExecutorTest;

import java.util.Date;

public class WorkerThread implements Runnable{
	private String command;
	
	public WorkerThread(String s){
		this.command = s;
	}

	@Override
	public void run() {
		System.out.println(Thread.currentThread().getName()+" Start .Time = "+new Date());
		processCommand();
		System.out.println(Thread.currentThread().getName()+" End .Time = "+new Date());
	}
	
	private void processCommand(){
		try{
			Thread.sleep(5000);
		}catch(InterruptedException e){
			e.printStackTrace();
		}
	}
	
	public String toString(){
		return this.command;
	} 
}


SheduledThreadPoolExecutorTest.java:(线程池测试类)

package test01.SheduledThreadPoolExecutorTest;

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

public class SheduledThreadPoolExecutorTest {

	/**
	 * @param args
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException {
		
		ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
		
		System.out.println("Current Time = "+new Date());
		
		for(int i=0;i<3;i++){
			Thread.sleep(1000);
			WorkerThread worker = new WorkerThread("do heavy processing.");
			scheduledThreadPool.schedule(worker, 10, TimeUnit.SECONDS);
		}
		
		Thread.sleep(10000);
		
		scheduledThreadPool.shutdown();
		while(!scheduledThreadPool.isTerminated()){
			//wait for all tasks to finish..
		}
		System.out.println("Finished all threads..");
	}

}


输出结果:   

Current Time = Thu Jul 12 09:20:09 CST 2018
pool-1-thread-1 Start .Time = Thu Jul 12 09:20:20 CST 2018
pool-1-thread-2 Start .Time = Thu Jul 12 09:20:21 CST 2018
pool-1-thread-3 Start .Time = Thu Jul 12 09:20:22 CST 2018
pool-1-thread-1 End .Time = Thu Jul 12 09:20:25 CST 2018
pool-1-thread-2 End .Time = Thu Jul 12 09:20:26 CST 2018
pool-1-thread-3 End .Time = Thu Jul 12 09:20:27 CST 2018
Finished all threads..

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

后台技术汇

对你的帮助,是对我的最好鼓励。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值