当前位置: 首页 > news >正文

二级网站建设方案模板目前做的比较好的法律网站有哪些

二级网站建设方案模板,目前做的比较好的法律网站有哪些,wordpress 主题跳转,关于做网站的ppt目录 介绍 Future介绍 CompletableFuture介绍 CompletableFuture常用的API介绍 常用的静态方法源码解析 runAsync 源码 案例 结果 supplyAsync 源码 案例 结果 规律 CompletableFuture获取返回值方法介绍 返回值区别 代码演示 返回结果 CompletableFuture其…目录 介绍 Future介绍 CompletableFuture介绍 CompletableFuture常用的API介绍 常用的静态方法源码解析 runAsync 源码 案例 结果  supplyAsync 源码 案例 结果 规律 CompletableFuture获取返回值方法介绍 返回值区别 代码演示 返回结果  CompletableFuture其他常用的API thenApply 源码  案例 结果 thenCompose 源码 案例 结果 thenAccept和thenRun  源码 案例 结果 complete 源码 案例 结果 whenComplete 源码 案例 结果  exceptionally  源码 案例 结果 whenCompleteexceptionally案例 结果 handle 源码 案例 结果 allof 源码 案例 结果 anyOf 源码 案例 结果 提示 介绍 Future介绍 了解CompletableFuture可以先了解Future,可以查看下面这篇文章 Callable、Future和FutrueTask详解-CSDN博客 CompletableFuture介绍 CompletableFuture是一个类主要实现了Future和ComletionStage两个接口。因此CompletableFuture包含了Futrure和CompletionStage的功能。 CompletableFuture常用的API介绍 官方推荐CompletableFutrue静态方法所以我们先介绍它的静态方法 常用的静态方法源码解析 线程池工具类 线程池工具类_java线程池工具类-CSDN博客 runAsync runAsync:无返回值可以自定义线程池如果没有自定义线程池默认使用ForkJoinPool线程池 源码 // 默认线程池 public static CompletableFutureVoid runAsync(Runnable runnable) {return asyncRunStage(asyncPool, runnable);} // 自定义线程池public static CompletableFutureVoid runAsync(Runnable runnable,Executor executor) {return asyncRunStage(screenExecutor(executor), runnable);} 案例 public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFuture.runAsync(() - {System.out.println(Thread.currentThread().getName() -------执行异步任务无返回值结果---------);});CompletableFuture.runAsync(() - {System.out.println(Thread.currentThread().getName() -----自定义线程执行异步任务无返回结果------);}, ThreadPoolUtils.getThreadPool());} 结果  ForkJoinPool.commonPool-worker-9  -------执行异步任务无返回值结果--------- myPool-0  -----自定义线程执行异步任务无返回结果------ supplyAsync supplyAsync:有返回值可以自定义线程池如果没有自定义线程池默认使用ForkJoinPool线程池 源码 //默认线程池public static U CompletableFutureU supplyAsync(SupplierU supplier) {return asyncSupplyStage(asyncPool, supplier);} // 自定义线程池 public static U CompletableFutureU supplyAsync(SupplierU supplier,Executor executor) {return asyncSupplyStage(screenExecutor(executor), supplier);} 案例 public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFutureString completableFuture1 CompletableFuture.supplyAsync(() - {System.out.println(Thread.currentThread().getName() ------执行异步任务,有返回值------);return 返回值1;});System.out.println(completableFuture1.get());CompletableFutureString completableFuture2 CompletableFuture.supplyAsync(() - {System.out.println(Thread.currentThread().getName() ------自定义线程池执行异步任务有返回值------);return 返回值2;}, ThreadPoolUtils.getThreadPool());System.out.println(completableFuture2.get());} 结果 ForkJoinPool.commonPool-worker-9  ------执行异步任务,有返回值------ 返回值1 myPool-0  ------自定义线程池执行异步任务有返回值------ 返回值2 规律 supply开头:可以返回异步执行的结果run开头:不会返回结果只是执行线程任务 CompletableFuture获取返回值方法介绍 join:返回结果或者抛出一个unchecked异常(CompletionException)不需要显式捕获异常get:此方法继承Future的get阻塞方法返回结果或者一个具体的异常(ExecutionException,InterruptedException)getNow:如果当前任务执行完成返回执行结果否则返回默认值 返回值区别 join与get区别在于join()返回计算的结果或者抛出一个异常而get会返回一个具体的异常getNow与join和get的区别getNow返回当前执行好的结果如果当前未执行完则返回设定好的默认值。 代码演示 public static void main(String[] args) {CompletableFuture completableFuture1 CompletableFuture.supplyAsync(() - {System.out.println(Thread.currentThread().getName() ------执行异步任务,有返回值------);return 返回值1;});try {System.out.println(get(): completableFuture1.get());} catch (InterruptedException e) {e.printStackTrace();} catch (ExecutionException e) {e.printStackTrace();}CompletableFuture completableFuture2 CompletableFuture.supplyAsync(() - {System.out.println(Thread.currentThread().getName() ------自定义线程池执行异步任务有返回值------);return 返回值2;}, ThreadPoolUtils.getThreadPool());System.out.println(join(): completableFuture2.join());CompletableFuture completableFuture3 CompletableFuture.supplyAsync(() - {System.out.println(Thread.currentThread().getName() ------自定义线程池执行异步任务有返回值------);try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}return 返回值3;}, ThreadPoolUtils.getThreadPool());System.out.println(------这一步直接返回默认值------);System.out.println(getNow(): completableFuture3.getNow(默认值));try {System.out.println(-----进行线程阻塞等待completableFuture3内容执行完成-----);System.out.println(get(): completableFuture3.get());} catch (ExecutionException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}System.out.println(-----当前异步任务已经执行完成返回结果-----);System.out.println(getNow(): completableFuture3.getNow(默认值));} 返回结果  ForkJoinPool.commonPool-worker-9  ------执行异步任务,有返回值------ get():返回值1 myPool-0  ------自定义线程池执行异步任务有返回值------ join():返回值2 ------这一步直接返回默认值------ getNow():默认值 -----进行线程阻塞等待completableFuture3内容执行完成----- myPool-1  ------自定义线程池执行异步任务有返回值------ get():返回值3 -----当前异步任务已经执行完成返回结果----- getNow():返回值3 CompletableFuture其他常用的API thenApply thenApply拿到上一步异步线程返回的结果进行后续处理。可以拿到上一次线程执行的返回结果并且可以一直传递下去。 源码  public U CompletableFutureU thenApply(Function? super T,? extends U fn) {return uniApplyStage(null, fn);} 案例 public static void main(String[] args) {String str 上海;String str2 深圳;CompletableFutureStringBuffer future CompletableFuture.supplyAsync(() - {return new StringBuffer(北京);// a是上一步异步线程返回的结果}, ThreadPoolUtils.getThreadPool()).thenApply(a - a.append(str)).thenApply(b - b.append(str2));try {System.out.println(future.get());} catch (InterruptedException e) {e.printStackTrace();} catch (ExecutionException e) {e.printStackTrace();}} 结果 北京上海深圳 thenCompose thenCompose方法功能跟thenApply相似都是会在上一个任务执行完成以后拿到上一步的执行结果进行后续处理不同的是两个方法的参数不一致并且thenCompose在执行的时候需要创建一个新的CompletableFuture。 源码 public U CompletableFutureU thenCompose(Function? super T, ? extends CompletionStageU fn) {return uniComposeStage(null, fn);} 案例 public static void main(String[] args) throws ExecutionException, InterruptedException {/*** supply直接返回会得到一个CompletableFutureString* 经过thenApply处理CompletableFutureString会转成一个CompletableFutureInteger* 但CompletableFuture是同一个*/CompletableFutureInteger future CompletableFuture.supplyAsync(() - {// 返回一个字符串return 北京;// a是上一步异步线程返回的结果}, ThreadPoolUtils.getThreadPool()).thenApply(a - {if (北京.equals(a)) {return 1;} else {return 0;}});System.out.println(future.get());CompletableFutureInteger completableFuture CompletableFuture.supplyAsync(() - {// 返回一个字符串return 北京;// 重新再创建一个CompleteFuture}, ThreadPoolUtils.getThreadPool()).thenCompose(a - CompletableFuture.supplyAsync(() - {if (北京.equals(a)) {return 1;} else {return 0;}}));System.out.println(completableFuture.get());} 结果 1 1  1 1 thenAccept和thenRun  thenAccept和thenRun:进行返回值回调。这样CompletableFuture就会没有返回值 源码 public CompletableFutureVoid thenAccept(Consumer? super T action) {return uniAcceptStage(null, action);}public CompletableFutureVoid thenRun(Runnable action) {return uniRunStage(null, action);}案例 public static void main(String[] args) throws ExecutionException, InterruptedException {//模拟计算 111CompletableFuture future CompletableFuture.supplyAsync(() - {return 1;}, ThreadPoolUtils.getThreadPool()).thenApply(a - a 1).thenAccept(b - System.out.println(b 1));System.out.println(thenAccept返回了什么? future.get());CompletableFuture future2 CompletableFuture.supplyAsync(() - {return 1;}, ThreadPoolUtils.getThreadPool()).thenApply(a - a 1).thenRun(()-{System.out.println(----------线程运行----------);});System.out.println(thenRun返回了什么? future2.get());} 结果 3 thenAccept返回了什么?null ----------线程运行---------- thenRun返回了什么?null 两者区别两者入参不同 thenRun参数是Runnable thenAccept参数是Consumer? super T actionthenAccept可以拿到上一步获取到的值。 complete complete()方法用于手动完成一个异步任务并设置其结果。一旦调用complete()方法CompleteFuture对象的状态会立即变成已完成。如果多个线程尝试调用complete()方法只有第一个成功的线程能够设置结果其他线程调用将被忽略 源码 public boolean complete(T value) {boolean triggered completeValue(value);postComplete();return triggered; } 案例 public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFutureString future CompletableFuture.supplyAsync(() - {return 10;}, ThreadPoolUtils.getThreadPool());future.complete(1000);future.complete(10000);//输出1000System.out.println(future.get());} 结果 调用complete以后:1000 whenComplete whenComplete是一个回调方法,会将CompletableFuture执行的结果和异常传递给它如果是正常执行则异常为null如果异常则get()方法会抛出异常。 源码 public CompletableFutureT whenComplete(BiConsumer? super T, ? super Throwable action) {return uniWhenCompleteStage(null, action);} 案例 public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFutureString completableFuture CompletableFuture.supplyAsync(() - {return 10;}, ThreadPoolUtils.getThreadPool()).whenComplete((v,e)-{System.out.println(获取到的异常:e);System.out.println(获取到的值:v);});// 这个地方会抛出异常System.out.println(whenComplete无异常回调以后以后: completableFuture.get());CompletableFutureString future CompletableFuture.supplyAsync(() - {int i 1 / 0;return 10;}, ThreadPoolUtils.getThreadPool()).whenComplete((v,e)-{System.out.println(获取到的异常:e);System.out.println(获取到的值:v);});// 这个地方会抛出异常System.out.println(whenComplete有异常回调以后: future.get());} 结果  获取到的异常:null 获取到的值:10 whenComplete无异常回调以后以后:10 获取到的异常:java.util.concurrent.CompletionException: java.lang.ArithmeticException: / by zero 获取到的值:null Exception in thread main java.util.concurrent.ExecutionException: java.lang.ArithmeticException: / by zeroat java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:357)at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1895)at com.common.base.util.CompletableFutureTest2.main(CompletableFutureTest2.java:30) Caused by: java.lang.ArithmeticException: / by zeroat com.common.base.util.CompletableFutureTest2.lambda$main$2(CompletableFutureTest2.java:23)at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)at java.lang.Thread.run(Thread.java:748)exceptionally  exceptionally()对CompletableFuture异常进行捕获处理并且设定一个返回值 源码 public CompletableFutureT exceptionally(FunctionThrowable, ? extends T fn) {return uniExceptionallyStage(fn);} 案例 public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFutureString future CompletableFuture.supplyAsync(() - {int i 1 / 0;return 10;}, ThreadPoolUtils.getThreadPool()).exceptionally(e - {//e.printStackTrace();System.out.println(执行的异常: e.getMessage());return 捕获异常以后自定义返回值;});System.out.println(捕获异常以后: future.get());} 结果 执行的异常:java.lang.ArithmeticException: / by zero 捕获异常以后:捕获异常以后自定义返回值 注意:大多数的时候我们通常都会把whenCompleteexceptionally结合起来一起使用 whenCompleteexceptionally案例 Callable、Future和FutrueTask详解-CSDN博客 有这么一个问题你的女朋友正在炒菜发现厨房没盐了需要你把新买的盐拿过来如果使用FutureTask那么会造成阻塞现在使用CompletFuture来解决这个问题 public static void main(String[] args) throws InterruptedException {System.out.println(------------女朋友开始炒菜----------------);try {CompletableFuture.supplyAsync(() - {return 让男朋友去买盐;}, ThreadPoolUtils.getThreadPool()).whenComplete((v, e) - {if (e null) {System.out.println(获取到任务: v);}}).exceptionally(e - {e.printStackTrace();System.out.println(执行异常: e.getCause());return null;});System.out.println(----------女朋友继续炒菜-----------------);Thread.sleep(2000);System.out.println(-----------------女朋友炒菜结束--------------);} catch (Exception e) {e.printStackTrace();}} 结果 ------------女朋友开始炒菜---------------- 获取到任务:让男朋友去买盐 ----------女朋友继续炒菜----------------- -----------------女朋友炒菜结束-------------- 通过上面代码我们可以知道主线程女朋友炒菜可以一步都到底不会出现线程阻塞的情况 handle handle()方法是一个回调方法跟whenComplete没有多大的区别唯一的区别是handle有返回值 源码 public U CompletableFutureU handle(BiFunction? super T, Throwable, ? extends U fn) {return uniHandleStage(null, fn);} 案例 public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFutureString future CompletableFuture.supplyAsync(() - {return 10;}, ThreadPoolUtils.getThreadPool()).handle((v, e) - {System.out.println(不存在异常的值 e);System.out.println(上一步返回值: v);return 100;});//这个地方会抛出异常System.out.println(无异常回调handle以后: future.get());CompletableFutureString completableFuture CompletableFuture.supplyAsync(() - {int i 1 / 0;return 10;}, ThreadPoolUtils.getThreadPool()).handle((v, e) - {System.out.println(不存在异常的值 e);System.out.println(上一步返回值: v);return 返回异常 e.getMessage();});//这个地方会抛出异常System.out.println(有异常回调handle以后: completableFuture.get());} 结果 不存在异常的值:null 上一步返回值:10 无异常回调handle以后:100 不存在异常的值:java.util.concurrent.CompletionException: java.lang.ArithmeticException: / by zero 上一步返回值:null 有异常回调handle以后:返回异常java.lang.ArithmeticException: / by zero allof allof是将多个CompletableFuture合并在一起 源码 public static CompletableFutureVoid allOf(CompletableFuture?... cfs) {return andTree(cfs, 0, cfs.length - 1);} 案例 public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFutureString future CompletableFuture.supplyAsync(() - {return 用户信息;}, ThreadPoolUtils.getThreadPool());//角色idCompletableFutureString future1 CompletableFuture.supplyAsync(() - {try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}return 角色信息;}, ThreadPoolUtils.getThreadPool());// 组合用户和角色idCompletableFuture future2 CompletableFuture.allOf(future, future1);CompletableFutureString completableFuture future2.thenApply(a - {return future.join() future1.join();});//通过回调函数获取对应的结果集System.out.println(等待两个线程执行完毕进行合并 completableFuture.join());} 结果 等待两个线程执行完毕进行合并用户信息 角色信息 anyOf 多个任务中那个任务先返回就先返回哪个结果  源码 public static CompletableFutureObject anyOf(CompletableFuture?... cfs) {return orTree(cfs, 0, cfs.length - 1);} 案例 public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFutureString future CompletableFuture.supplyAsync(() - {return 用户信息;}, ThreadPoolUtils.getThreadPool());//角色idCompletableFutureString future1 CompletableFuture.supplyAsync(() - {try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}return 角色信息;}, ThreadPoolUtils.getThreadPool());// 组合用户和角色idCompletableFuture future2 CompletableFuture.anyOf(future, future1);System.out.println(anyOf谁先执行完谁就先返回: future2.join());} 结果 anyOf谁先执行完谁就先返回:用户信息 提示 CompletableFuture提供了很多方法上面的方法都是CompletableFuture提供的同步方法如果在上面的方法后缀加上Async那么成了调用它的异步方法比如thenApplyAsync()。 参考文章:CompletableFuture使用详解 - 简书
http://wiki.neutronadmin.com/news/148263/

相关文章:

  • 怎么查网站是不是正规wordpress有多少种语言
  • 青岛网站建设保山分销商城模式系统开发
  • 自己创建的网站instagram wordpress
  • 怡清源企业网站建设可行性分析广州网站设计哪个好
  • 怎么样建网站啊网站建设 服务流程
  • 网站验收模版自助建站系统破解版
  • 禅城区网站建设互联网推广好做吗
  • 学校网页网站模板wordpress 国内视频网站
  • 网站推广的目标是什么北京装修公司排名前十名
  • 建立问答类的网站wordpress文章模板如何修改
  • 管局备案网站做新网站怎样提交360
  • 上海住房和城乡建设厅网站如何网站建设的方案
  • 珠海做网站技校平面设计主要做什么
  • 郑州专业的网站公司wordpress作者 页面
  • 免费做淘宝联盟网站网络营销专业好就业吗
  • 网站运营解决方案wordpress转成中文
  • 网站建设公司浙江北京的制作网站的公司
  • 添加建设银行的网站企业级网站开发平台
  • 国外购物网站系统专门做动漫的网站吗
  • 自己做的简单网站下载深圳公司查询
  • 青岛网站搭建公司哪家好营销型网站是什么意思
  • 旅游网站开发工具自己做网站前期困难吗
  • 我找别人做的网站现在不管了怎么办宁波人流医院
  • 做网站服务好网络运营工资大概多少
  • 佛山微信网站建设多少钱wordpress 能源插件
  • 用python做音乐网站怎么让公司网站显示官网
  • 中国内销做哪个网站找百度做的网站可以过户
  • 企业网站开发php网站qq获取
  • 网站死链接检查移动网站 图片优化
  • 有了空间和域名 网站容易做吗织梦做的网站图片路径在哪里