标签:并发 实例 submit proc one target 实现 ali fan
public class chuju implements Callable<Boolean>{ @Override public Boolean call() throws Exception { try{ System.out.println("买厨具"); Thread.sleep(3000); System.out.println("买好厨具"); }catch (InterruptedException e){ e.printStackTrace(); } return true; } }
public class shicai implements Callable<Boolean>{ @Override public Boolean call() throws Exception { try{ System.out.println("买食材"); Thread.sleep(1000); System.out.println("买好食材"); }catch(InterruptedException e){ e.printStackTrace(); } return true; } }
public class zuofan implements Callable<Boolean>{ @Override public Boolean call() throws Exception { try{ System.out.println("做饭"); Thread.sleep(5000); System.out.println("做好饭"); }catch (InterruptedException e){ e.printStackTrace(); } return true; } }
public class Main { public static void main(String[] args) { ExecutorService es = Executors.newCachedThreadPool(); chuju cj = new chuju(); shicai sc = new shicai(); Future<Boolean> f1 = es.submit(cj); Future<Boolean> f2 = es.submit(sc); try{ Boolean b1 = f1.get();//会阻塞当前线程 Boolean b2 = f2.get(); System.out.println(b1); System.out.println(b2); if(b1 && b2){ zuofan zf = new zuofan(); es.submit(zf); } }catch(InterruptedException e){ e.printStackTrace(); }catch (ExecutionException e){ e.printStackTrace(); } es.shutdown(); } }
标签:并发 实例 submit proc one target 实现 ali fan
原文地址:https://www.cnblogs.com/myxcf/p/9959870.html