线程之Callable与Future
Trackback:http://blog.csdn.net/qian_348840260/archive/2010/01/19/5214488.aspxpackage cn.com.axis.thread;import java.util.concurrent.Callable;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;/** * Copyright(c), 2011-2100 * <p>Cwc System * @description:use Callable class define a task and start it,use Future class to get output result and stop it; * 1:Callable is similar to Runnable interface,we can implement callable or runnable interface to execute task by other thread; * difference between callable and runnable: * a:callable defines method:call(),but runnable defines method:run(); * b:call() can has return value,but run() couldn't; * c:call() can throw exception,but run() couldn't; * 2:Future means result of asynchronous calculate,it supports that method if complete,in order to(so as to)wait to complete, * and search calculate result,cancel() from Future will cancel task and not execute,this method has a boolean parameter, * parameter result is true:interrupt task ofexecuting immediately; * parameter result is false:allow running task run complete,get() will wait to calculate complete and get calculate result * @file name: cn.com.axis.thread.CallableAndFuture.java * @create: by author axis on Mar 5, 20115:13:25 PM * @since:JDK1.6u21 * @see: */public class CallableAndFuture {//a task class of user-defined,implements Callable@SuppressWarnings("unchecked")public static class SelfCallable implements Callable{private int flag = 0;public SelfCallable (int flag){this.flag = flag;}public String call() throws Exception{if (this.flag == 0) {return "flag = 0";}//do unlimited cycleif (this.flag == 1) {try {while (true) {System.out.println("Loop...");Thread.sleep(2000);}} catch (InterruptedException e) {System.out.println(e.toString()+"\r\nInterrupt!");e.printStackTrace();}return "flag = false";}throw new Exception("Bad flag value,not zero and one");}}@SuppressWarnings("unchecked")public static void main(String[] args) {//define threetask of callable typeSelfCallable callableOne = new SelfCallable(0);SelfCallable callableTwo = new SelfCallable(1);SelfCallable callableThree = new SelfCallable(2);//create a service of execute taskExecutorService es = Executors.newFixedThreadPool(3);try {//submit and execute task,return a future object when start task;//you can operator this future object if you want to get result or exceptionwhen execute task;Future futureOne = es.submit(callableOne);//get result of the first task,if call get(),the current thread will wait this task execute complete and execute go onSystem.out.println("callableOne: "+ futureOne.get());Future futureTwo = es.submit(callableTwo);//wait five seconds and stop the second task,becausethe second task is unlimited cycleThread.sleep(5000);System.out.println("callableTwo: "+futureTwo.cancel(true));//get output of the third task,because the third task will cause exceptionFuture futureThree = es.submit(callableThree);System.out.println("callableThree: "+futureThree.get());} catch (Exception e) {System.out.println(e.toString());e.printStackTrace();}//stop task immediately and execute servicees.shutdown();}}
http://dl.iteye.com/upload/picture/pic/83043/0009eb71-883f-3ba3-94e9-b09b937c0ef6.jpg
页:
[1]