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

政府网站建设高端会议上海seo公司排名

政府网站建设高端会议,上海seo公司排名,企业邮箱费用,代客做网站目录 目标 Netty版本 Netty官方API 三者之间的关系 基本使用方法 java.util.concurrent.Future io.netty.util.concurrent.Future io.netty.util.concurrent.Promise 目标 了解io.netty.util.concurrent.Promise与io.netty.util.concurrent.Future的基本使用方法。了解…

目录

目标

Netty版本

Netty官方API

三者之间的关系

基本使用方法

java.util.concurrent.Future

io.netty.util.concurrent.Future

io.netty.util.concurrent.Promise


目标

  • 了解io.netty.util.concurrent.Promise与io.netty.util.concurrent.Future的基本使用方法。
  • 了解java.util.concurrent.Future、io.netty.util.concurrent.Promise,io.netty.util.concurrent.Future之间的关系。

Netty版本

        <dependency><groupId>io.netty</groupId><artifactId>netty-all</artifactId><version>4.1.87.Final</version></dependency>

Netty官方API

Netty API Reference (4.1.89.Final)icon-default.png?t=N176https://netty.io/4.1/api/index.html


三者之间的关系

区别

  • jdk自带的Future只能同步等待结果。
  • netty自带的Future能同步等待结果,也可以用异步的方式(如:使用addListener方法设置回调方法)等待结果。
  • Promise有Future的所有功能,脱离任务独立存在(可以主动创建并赋结果),只作为线程之间传递结果的容器。

关联

Promise extends netty自带的Future extends jdk自带的Future


基本使用方法

java.util.concurrent.Future

package com.ctx.netty;import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.*;@Slf4j
public class JavaFuture {public static void main(String[] args) {ExecutorService executor = Executors.newFixedThreadPool(2);Future<String> future = executor.submit(new Callable<String>() {@Overridepublic String call() throws Exception {Thread.sleep(1000);return "result";}});//同步阻塞返回结果。try {log.info("返回结果值:{}",future.get());} catch (InterruptedException e) {throw new RuntimeException(e);} catch (ExecutionException e) {throw new RuntimeException(e);}}
}

io.netty.util.concurrent.Future

package com.ctx.netty;import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;@Slf4j
public class NettyFuture {public static void main(String[] args) {NioEventLoopGroup eventExecutors = new NioEventLoopGroup();EventLoop eventLoop = eventExecutors.next();Future<Map<String, Object>> future = eventLoop.submit(new Callable<Map<String, Object>>() {@Overridepublic Map<String, Object> call() throws Exception {Thread.sleep(1000);Map<String, Object> map = new HashMap<>();map.put("name", "zhangsan");return map;}});new NettyFuture().getNow(future);}/*** 同步阻塞等待结果。* @param future*/public void get(Future<Map<String, Object>> future){try {log.info("结果是:"+future.get());} catch (InterruptedException e) {throw new RuntimeException(e);} catch (ExecutionException e) {throw new RuntimeException(e);}}/***异步方式等待结果。* @param future*/public void getNow(Future<Map<String, Object>> future){future.addListener(new GenericFutureListener<Future<? super Map<String, Object>>>() {//拿到结果以后回调方法。所以此时执行get()还是getNow()是一样的效果。@Overridepublic void operationComplete(Future<? super Map<String, Object>> future) throws Exception {log.info("结果是:"+future.getNow());}});}
}

io.netty.util.concurrent.Promise

package com.ctx.netty;import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultPromise;
import io.netty.util.concurrent.Promise;
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;@Slf4j
public class NettyPromise {public static void main(String[] args) {NioEventLoopGroup eventExecutors = new NioEventLoopGroup();EventLoop eventLoop = eventExecutors.next();//自定义类型,用于填充结果。Promise<Map<String, Object>> promise = new DefaultPromise<>(eventLoop);new Thread(()->{try {Thread.sleep(1000);Map<String, Object> map = new HashMap<>();map.put("name","zhangsan");promise.setSuccess(map);} catch (Exception e) {promise.setFailure(e);}}).start();try {Map<String, Object> map = promise.get();map.forEach((k,v)->{System.out.println(k+"="+v);});} catch (InterruptedException e) {throw new RuntimeException(e);} catch (ExecutionException e) {throw new RuntimeException(e);}}
}

http://www.hkea.cn/news/202363/

相关文章:

  • 网站用oracle做数据库福州seo推广服务
  • 康保县城乡建设委员会网站营销型网站重要特点是
  • 手机做网站的步骤跨境电商有哪些平台
  • 请人做网站要多少网络事件营销
  • 网站页脚有什么作用厦门seo哪家强
  • 东莞百度提升优化优化推广网站推荐
  • 查企业网站有哪些站长统计app软件
  • 做a高清视频在线观看网站济源新站seo关键词排名推广
  • 刚做的网站怎么搜索不出来百度seo收录软件
  • 视频拍摄app站长工具seo综合查询广告
  • 新闻单位建设网站的意义武汉seo推广优化
  • 低价网站公司软文怎么写
  • 东莞市建设公共交易中心网站百度官网首页
  • 如何建立的网站能争钱优化营商环境 助推高质量发展
  • 做百度网站营销型网站建设排名
  • 网站域名被黑国际新闻最新消息战争
  • 苏州网站开发公司济南兴田德润厉害吗网络自动推广软件
  • 广药网站建设试卷株洲最新今日头条
  • 网站建设管理考核办法微信推广平台怎么做
  • 网站新闻模块代码网络推广有哪些常见的推广方法
  • 合肥大型网站如何推广普通话
  • 高端网站制作软件怎么样推广自己的店铺和产品
  • 无障碍浏览网站怎么做关键词seo排名优化推荐
  • wordpress 247seo推广系统
  • 做深圳门户网站起什么名字好泰州seo外包公司
  • 网站视频上传怎么做百度站长平台论坛
  • wordpress农业模板下载小时seo
  • 做网站语言排名2018发帖推广哪个平台好
  • 销氪crmseo入门讲解
  • 蒙阴哪有做淘宝网站的钓鱼网站制作教程