Java7新特性(四)并发 7 ScheduledThreadPool
来源:互联网 发布:js的prototype是什么 编辑:程序博客网 时间:2024/06/06 04:19
本文主要根据《Java程序员修炼之道》整理的代码笔记片段
工作单元
public class WorkUnit<T> { private final T workUnit; public T getWork() { return workUnit; } public WorkUnit(T _workUnit) { workUnit = _workUnit; }}
计划线程池(启动、关闭、执行队列中工作单元)
public class ScheduledThreadPoolExecutorClass {private ScheduledExecutorService stpe;private ScheduledFuture<?> hndl;BlockingQueue<WorkUnit<String>> lbq =new LinkedBlockingQueue<>();private void run(){stpe = Executors.newScheduledThreadPool(2);final Runnable msgReader = new Runnable() {@Overridepublic void run() {String nextMsg = lbq.poll().getWork();if(nextMsg != null){System.out.println("Msg recvd: "+ nextMsg);}}};hndl =stpe.scheduleAtFixedRate(msgReader, 10, 10, TimeUnit.MILLISECONDS);}private void cancel(){final ScheduledFuture<?> myHndl = hndl;stpe.schedule(new Runnable() {@Overridepublic void run() {myHndl.cancel(true);}}, 10, TimeUnit.MILLISECONDS);}public static void main(String[] args) {ScheduledThreadPoolExecutorClass s = new ScheduledThreadPoolExecutorClass();for (int i = 0; i < 10; i++) {s.lbq.add(new WorkUnit<String>(i+""));}s.run();for (int i = 10; i < 20; i++) {s.lbq.add(new WorkUnit<String>(i+""));}}}
think in java 实例 Callable
/*The submit( ) method produces a Future object, parameterized for the particular type of result returned by the Callable. You can query the Future with isDone( ) to see if it has completed. When the task is completed and has a result, you can call get( ) to fetch the result. You can simply call get( ) without checking isDone( ),in which case get( ) will block until the result is ready. You can also call get( ) with a timeout, or isDone( ) to see if the task has completed,before trying to call get( ) to fetch the result.The overloaded Executors.callable( ) method takes a Runnable and produces a Callable. ExecutorService has some "invoke" methods that run collections of Callable objects.*/public class CallableDemo {public static void main(String[] args) {ExecutorService exec = Executors.newCachedThreadPool();ArrayList<Future<String>> results = new ArrayList<Future<String>>();for (int i = 0; i < 10; i++)results.add(exec.submit(new TaskWithResult(i)));for (Future<String> fs : results)try {// get() blocks until completion:System.out.println(fs.get());} catch (InterruptedException e) {System.out.println(e);return;} catch (ExecutionException e) {System.out.println(e);} finally {exec.shutdown();}}}class TaskWithResult implements Callable<String> {private int id;public TaskWithResult(int id) {this.id = id;}public String call() {return "result of TaskWithResult " + id;}}
- Java7新特性(四)并发 7 ScheduledThreadPool
- Java7新特性(四)并发 3 CountDownLatch计数器对象
- Java7新特性(四)并发 4 Atomic、ConcurrentHashMap对象
- Java7新特性(四)并发 5 CopyOnWriteArrayList对象
- Java7新特性(四)并发 6 TransferQueue对象
- Java7新特性(四)并发 1 不可变对象及构造器
- Java7新特性(四)并发 2 Lock对象处理死锁
- Java7新特性(四)并发 8 forkjoin分支合并框架
- java7新特性(转载)
- [笔记][Java7并发编程实战手册]5.Fork\Join(Java1.7新特性)框架
- java7新特性——使用ThreadLocalRandom产生并发随机数
- java7新特性——使用ThreadLocalRandom产生并发随机数
- java7新特性——使用ThreadLocalRandom产生并发随机数
- java7新特性之一 7大新功能
- Java7新特性(一)Coin
- Java7新特性(二)IO
- Java7新特性(三)DI
- Java7新特性(二)IO
- CAS单点登录
- 风机叶轮平衡机概述
- wamp覆盖安装后出现的一点问题
- 清空SQL Server数据库中所有表数据的方法
- STM32 IAP
- Java7新特性(四)并发 7 ScheduledThreadPool
- 将jre限制不能用的包启用方法。 sun.net.ftp.FtpClient
- [Hibernate]a different object with the same identifier value was already associated with the session
- iOS开发之详解剪贴板
- delphi7的基础语法
- Java Socket实战之三 传输对象
- Google chrome浏览器的十个秘密页面
- YUV(一)-数据格式分析
- android开发 drawable(hdpi,mdpi,ldpi)的区别