1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
package concurrent.future;
import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.FutureTask;
public class RealData2 implements Callable<String> {
String pram;
public RealData2(String pram) { super(); this.pram = pram; }
public String call() throws Exception { StringBuilder sb = new StringBuilder(); for (int i = 0; i <= 10; i++) { sb.append(pram + " " + i + " "); try { Thread.sleep(1 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } } return sb.toString(); }
public static void main(String[] args) throws InterruptedException, ExecutionException { RealData2 data = new RealData2(" lee is good"); FutureTask<String> task = new FutureTask<String>(data); ExecutorService service = Executors.newSingleThreadExecutor(); service.submit(task); System.out.println(System.currentTimeMillis() + " 请求完毕 "); String result = task.get(); System.out.println(System.currentTimeMillis() + " 结果 " + result); service.shutdown(); } }
|
近期评论