Java并发编程之线程管理(Executor框架15)
来源:互联网 发布:微课视频剪辑软件 编辑:程序博客网 时间:2024/06/01 09:09
4.5指定一段时间运行并发任务
当你发送一个任务给指定的executor时,它依据executor的配置来迅速被运行。当你对线程的运行不太感兴趣,只需要它快速的运行就可以了,这就是它的使用场合。当你想间歇地执行一个任务或者定期地执行一个任务。对于这些目的,Executor框架提供了一个ScheduledThreadPoolExecutor类。请看下面实例实例代码。
定义Task类,实现一个简单的逻辑。
import java.util.Date;import java.util.concurrent.Callable;import java.util.concurrent.Executors;importjava.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit; /** * This class implements the task ofthis example. Writes a * message to the console with theactual date and returns the * 'Hello, world' string * */public class Task implements Callable<String> { /** * Name of the task */ private Stringname; /** * Constructor of the class * @param name Name of the task */ public Task(String name) { this.name=name; } /** * Main method of the task. Writes a message tothe console with * the actual date and returns the 'Helloworld' string */ @Override public String call()throws Exception { System.out.printf("%s: Starting at : %s\n",name,new Date()); return"Hello, world"; } /** * Main method of the example * @param args */ public staticvoidmain(String[] args) { // Create a ScheduledThreadPoolExecutor ScheduledExecutorService executor=(ScheduledExecutorService)Executors.newScheduledThreadPool(1); System.out.printf("Main: Starting at: %s\n",new Date()); // Send the tasks to the executor with the specified delay for (int i=0; i<5; i++) { Task task=new Task("Task "+i); executor.schedule(task,i+1 ,TimeUnit.SECONDS); } // Finish the executor executor.shutdown(); // Waits for the finalization of the executor try { executor.awaitTermination(1,TimeUnit.DAYS); } catch (InterruptedException e) { e.printStackTrace(); } // Writes the finalization message System.out.printf("Core: Ends at: %s\n",new Date()); }}
运行结果:
Main:Starting at: Sun Mar 30 23:03:12 CST 2014Task0: Starting at : Sun Mar 30 23:03:13 CST 2014Task1: Starting at : Sun Mar 30 23:03:14 CST 2014Task2: Starting at : Sun Mar 30 23:03:15 CST 2014Task3: Starting at : Sun Mar 30 23:03:16 CST 2014Task4: Starting at : Sun Mar 30 23:03:17 CST 2014Core:Ends at: Sun Mar 30 23:03:17 CST 2014
0 0
- Java并发编程之线程管理(Executor框架15)
- Java并发编程之线程管理(Executor框架11)
- Java并发编程之线程管理(Executor框架12)
- Java并发编程之线程管理(Executor框架13)
- Java并发编程之线程管理(Executor框架14)
- Java并发编程之线程管理(Executor框架16)
- Java并发编程-Executor框架之ScheduledThreadPoolExecutor
- Java并发编程-Executor框架之CompletionService
- 【Java并发编程】之十九:并发新特性—Executor框架与线程池(含代码)
- 【Java并发编程】之十九:并发新特性—Executor框架与线程池(含代码)
- 【Java并发编程】之十九:并发新特性—Executor框架与线程池(含代码)
- 【Java并发编程】之十九:并发新特性—Executor框架与线程池(含代码)(r)
- 【Java并发编程】之十九:并发新特性—Executor框架与线程池(含代码)
- Java并发编程之十九:并发新特性—Executor框架与线程池(含代码)
- 【Java并发编程】之十九:并发新特性—Executor框架与线程池
- JAVA随笔之线程编程(Executor框架)
- java并发编程-Executor框架
- java并发编程-Executor框架
- 关于Tomcat搭建服务器需要80端口问题的解决方案以及80端口被NT KERNEL & SYSTEM ,pid4占用解决方法
- ngnix+uwsgi+django+python
- Codeforces Round #239 (Div. 2)
- gdb调试
- spring mvc+mybatis+mysql项目
- Java并发编程之线程管理(Executor框架15)
- hdu1004
- 从零开始学习cpld(1)
- 单例模式以及单例模式与静态类的对比
- 三门问题
- 算法导论学习之归并排序
- OSX: Garageband/LogicPro各种安装包下载
- python----异常、属性
- 基础加强-IntroSpector(内省)