zoukankan      html  css  js  c++  java
  • 多线程异步运行返回与非返回值




    //无返回值
    CompletableFuture completableFuture0=CompletableFuture.runAsync(());
    //无返回值执行器
    CompletableFuture completableFuture0_0=CompletableFuture.runAsync(()->{
                System.out.println("如果帅有罪,请把我埋葬");
            },executor);
    //返回值
     CompletableFuture<Integer> completableFuture2 = CompletableFuture.supplyAsync(()
    //返回值执行器执行
     CompletableFuture<Long> future = CompletableFuture.supplyAsync(System::currentTimeMillis,executor0);
    

    //11111 //无返回值 异步等待执行 什么时候执行完什么时候返回 CompletableFuture completableFuture0=CompletableFuture.runAsync(()->{ try { TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace(); } System.out.println("没有返回值》》》》》》》》》》》"); }); System.out.println("00000"); completableFuture0.get();//获取阻塞执行结果

    //有返回值的回调,supplyAsync():里面的参数是Suplier供给型函数接口,有输出,没有输入 CompletableFuture<Integer> completableFuture1 = CompletableFuture.supplyAsync(() -> { System.out.println(Thread.currentThread().getName() + "supplyAsync=> 有返回值"); // 表示执行了异步任务 try { TimeUnit.SECONDS.sleep(15); } catch (InterruptedException e) { e.printStackTrace(); } return 1024; //直接返回 1024 }); ExecutorService executor = Executors.newCachedThreadPool(); CompletableFuture completableFuture0_0=CompletableFuture.runAsync(()->{ System.out.println("如果帅有罪,请把我埋葬"); },executor); completableFuture0_0.get(); //2222 // BiConsumer:是消费型接口的改变版,可以传递2个参数 void accept(T t, U u); // 这两个参数,t表示:正常的返回结果,u表示:错误信息 // exceptionally方法:参数为Function<Throwable, ? extends T> fn),可以有输入也可以有返回值 // 如果有错误就将错误输入,输出 // get()方法:获取异步回调返回的参数 System.out.println(completableFuture1.whenComplete((t, u)->{ System.out.println("t=>" + t); System.out.println("u=>" + u); }).exceptionally((e) -> { System.out.println(e.getMessage()); return 500; // 可以获取到错误的返回值 }).get()); Integer integer; integer = completableFuture1.whenComplete((s, u) -> { System.out.println("t=>" + s); System.out.println("u=>" + u); }).exceptionally((e) -> { System.out.println(e.getMessage()); return 500; // 可以获取到错误的返回值 }).get(10,TimeUnit.SECONDS); System.out.println(integer); // //3333 try { //最多等待10s返回,超过10s抛出超时错误 Integer integer = completableFuture1.get(10, TimeUnit.SECONDS); System.out.println(integer); } catch (TimeoutException e) { e.printStackTrace(); } //4444 //模拟错误返回值 CompletableFuture<Integer> completableFuture2 = CompletableFuture.supplyAsync(() -> { int i=1/0; return 1024; //直接返回 1024 }); /* BiConsumer:是消费型接口的改变版,可以传递2个参数 void accept(T t, U u); 这两个参数,t表示:正常的返回结果,u表示:错误信息 exceptionally方法:参数为Function<Throwable, ? extends T> fn),可以有输入也可以有返回值 如果有错误就将错误输入,输出 get()方法:获取异步回调返回的参数 */ Integer integer0 = completableFuture2.whenComplete((t, u) -> { System.out.println("t=>" + t); System.out.println("u=>" + u); }).exceptionally((e) -> { System.out.println(e.getMessage()); return 500; // 可以获取到错误的返回值 }).get(); System.out.println(integer0); ExecutorService executor0 = Executors.newCachedThreadPool(); CompletableFuture<Long> future = CompletableFuture.supplyAsync(System::currentTimeMillis,executor0); System.out.println(future.get());
    一点点学习,一丝丝进步。不懈怠,才不会被时代淘汰
  • 相关阅读:
    11个重要的数据库设计规则
    CentOS 6.8 新安装系统的网络IP配置(转载)
    WebView根据加载的内容来控制其高度
    遗传算法
    Selenium: Trying to log in with cookies and get the errorMessage
    用Tesseract训练验证码遇到的问题
    利用jTessBoxEditor工具进行Tesseract-OCR样本训练
    Tesseract处理背景渐变的图片
    XPath语法
    在Python中用Selenium执行JavaScript
  • 原文地址:https://www.cnblogs.com/wangbiaohistory/p/15046744.html
Copyright © 2011-2022 走看看