Java并发编程举例Runnable, Callable, Future, FutureTask, CompletionService
生活随笔
收集整理的這篇文章主要介紹了
Java并发编程举例Runnable, Callable, Future, FutureTask, CompletionService
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import java.util.concurrent.*;/*** Created by chenh on 2017/3/23.*/
public class ConcurrentDemo {// 沒有返回值//public interface Runnablepublic class RunnableTask implements Runnable{public void run() {System.out.println("run");}}//有返回值//public interface Callable<V>public class CallableTask implements Callable<String> {public String call() throws Exception {System.out.println("call");return "call";}}public void testRunnableTask(){ExecutorService executorService = Executors.newCachedThreadPool();//public interface Future<V>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<V> extends Runnable, Future<V>//public class FutureTask<V> implements RunnableFuture<V>FutureTask<String> futureTask = new FutureTask<String>(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<V> extends Runnable, Future<V>//public class FutureTask<V> implements RunnableFuture<V>FutureTask<String> futureTask = new FutureTask<String>(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<Integer>{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<V> implements CompletionService<V>CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(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
總結
以上是生活随笔為你收集整理的Java并发编程举例Runnable, Callable, Future, FutureTask, CompletionService的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于Win32Api创建窗口程序
- 下一篇: ubuntu使用KVM创建虚拟机