博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java并发编程举例Runnable, Callable, Future, FutureTask, CompletionService
阅读量:2389 次
发布时间:2019-05-10

本文共 3902 字,大约阅读时间需要 13 分钟。

import java.util.concurrent.*;/** * Created by chenh on 2017/3/23. */public class ConcurrentDemo {    // 没有返回值    //public interface Runnable    public class RunnableTask implements Runnable{        public void run() {            System.out.println("run");        }    }    //有返回值    //public interface Callable
public class CallableTask implements Callable
{ public String call() throws Exception { System.out.println("call"); return "call"; } } public void testRunnableTask(){ ExecutorService executorService = Executors.newCachedThreadPool(); //public interface Future
Future
future = executorService.submit(new RunnableTask()); try { System.out.println("testRunnableTask: " + future.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } executorService.shutdown(); //new Thread(new RunnableTask()).start() } public void testCallableTask() { ExecutorService executorService = Executors.newCachedThreadPool(); Future
future = executorService.submit(new CallableTask()); try { System.out.println("testCallableTask: " + future.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } executorService.shutdown(); } public void testFutureTask1(){ ExecutorService executorService = Executors.newCachedThreadPool(); //public interface RunnableFuture
extends Runnable, Future
//public class FutureTask
implements RunnableFuture
FutureTask
futureTask = new FutureTask
(new RunnableTask(), "result"); Future
future = executorService.submit(futureTask); try { System.out.println("testFutureTask: " + future.get()); System.out.println("testFutureTask: " + futureTask.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } executorService.shutdown(); } public void testFutureTask2(){ ExecutorService executorService = Executors.newCachedThreadPool(); //public interface RunnableFuture
extends Runnable, Future
//public class FutureTask
implements RunnableFuture
FutureTask
futureTask = new FutureTask
(new CallableTask()); Future
future = executorService.submit(futureTask); try { System.out.println("testFutureTask: " + future.get()); System.out.println("testFutureTask: " + futureTask.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } executorService.shutdown(); } public class CallableTask2 implements Callable
{ int id; public CallableTask2(int id){ this.id = id; } public Integer call() throws Exception { return id; } } public void testCompletionService(){ ExecutorService executorService = Executors.newCachedThreadPool(); //public class ExecutorCompletionService
implements CompletionService
CompletionService
completionService = new ExecutorCompletionService
(executorService); for (int i=0; i<10; i++) { completionService.submit(new CallableTask2(i)); } for (int i=0; i<10; i++) { try { System.out.println(completionService.take().get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } executorService.shutdown(); } public static void main(String[] args){ ConcurrentDemo demo = new ConcurrentDemo(); demo.testRunnableTask(); demo.testCallableTask(); demo.testFutureTask1(); demo.testFutureTask2(); demo.testCompletionService(); }}

运行效果

run

testRunnableTask: null
call
testCallableTask: call
run
testFutureTask: null
testFutureTask: result
call
testFutureTask: null
testFutureTask: call
0
1
4
5
2
7
8
3
6
9

转载地址:http://rltab.baihongyu.com/

你可能感兴趣的文章
某大型网站的内核TCP/ip优化脚本
查看>>
Defeating SSL using SSLStrip (Marlinspike Blackhat)
查看>>
大型网站数据库架构
查看>>
rdp 安全策略
查看>>
Threat Intelligence Quotient Test
查看>>
Cisco路由器上防止DDOS的一些建议
查看>>
系统安全防护之UNIX下入侵检测方法
查看>>
域控渗透技巧
查看>>
Minion security project and 分布式nmap
查看>>
防火墙相关
查看>>
网络性能测试工具Iperf上手指南
查看>>
opensecuritytraining video
查看>>
collective intelligence framework
查看>>
2015年关注的技术书籍
查看>>
windows 2003 server 记录远程桌面的连接登录日志和修改3389连接端口方法
查看>>
samhain:比较变态的入侵检测系统
查看>>
Linux psacct文档
查看>>
使用setuptools自动安装python模块
查看>>
python IDE环境
查看>>
传说中的windows加固 -.... -
查看>>