package thread; import java.util.concurrent.*; /** * @auto dh * @create 2020-03-29-0:04 */class Th0009 implements Callable<Integer>{ private int sum=0; public Integer call(){ for(int i=1;i<=100;i++){ sum+=i; } return sum; }}class Th009 implements Runnable { public void run() { for (int i = 0; i <= 100; i++) { if (i % 2 == 0) { System.out.println(Thread.currentThread().getName()+":"+i); } } }} public class Thread009 { public static void main(String[] args) { Th009 th=new Th009(); Th0009 t1=new Th0009();// 1、通过Executors工具类来创建线程池对象 ExecutorService executorService = Executors.newFixedThreadPool(12);// 2、execute()用来处理Runnable接口 executorService.execute(th);// submit()用来处理Callable接口 FutureTask future= new FutureTask(t1); Future future1= executorService.submit(future); try {// 通过FutureTask对象的get()方法获取线程的返回值 System.out.println("当前线程的返回值:"+future.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); }// 3、关闭线程池 executorService.shutdown(); }}
原文地址:https://www.cnblogs.com/kukai/p/12590465.html
时间: 2024-10-11 07:47:45