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

全国企业查询系统信息廊坊视频优化推广

全国企业查询系统信息,廊坊视频优化推广,男女做暖暖的试看网站大全,社区网站建设方案博主前些天发现了一个巨牛的人工智能学习网站#xff0c;通俗易懂#xff0c;风趣幽默#xff0c;忍不住也分享一下给大家 #x1f449;点击跳转到教程 前言#xff1a;源码阅读基于okhttp:3.10.0 Android中OkHttp源码阅读二(责任链模式) implementation com.squareup.o… 博主前些天发现了一个巨牛的人工智能学习网站通俗易懂风趣幽默忍不住也分享一下给大家 点击跳转到教程 前言源码阅读基于okhttp:3.10.0 Android中OkHttp源码阅读二(责任链模式) implementation com.squareup.okhttp3:okhttp:3.10.01、首先回顾OkHttp的使用 public class MainActivity extends RxActivity {Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}/*** OkHttp的使用*/private static void okHttpUseAction() {//通过构建者设计模式得到OkHttpClientOkHttpClient okHttpClient new OkHttpClient.Builder().build();//get请求 构建者模式拿到requestRequest request new Request.Builder().url(https://www.baidu.com/).get().build();//Call call RealCallCall call okHttpClient.newCall(request); // call.cancel();//取消请求//同步方法我们需要自己开启子线程 耗时 // try { // Response response call.execute(); // String string response.body().string(); // InputStream inputStream response.body().byteStream(); // Reader reader response.body().charStream(); // } catch (IOException e) { // e.printStackTrace(); // }//异步方法call.enqueue(new Callback() {Overridepublic void onFailure(Call call, IOException e) {System.out.println(请求失败...);}Overridepublic void onResponse(Call call, Response response) throws IOException {String string response.body().string();System.out.println(请求完成 string); // InputStream inputStream response.body().byteStream(); // Reader reader response.body().charStream();}});}public static void main(String[] args) {okHttpUseAction();} }2、OkHttp源码阅读之线程池详解 /*** Author: ly* Date: 2023/9/3* Description: 线程池的使用*/ public class MyThreadPool {public static void main(String[] args) {//比较耗性能开启子线程然后回收new Thread() {Overridepublic void run() {super.run();}}.start();//java 1.5 线程如何复用 线程池复用//子线程//需要一份工作招聘工作员工完成工作后解聘//需要一份工作招聘工作员工完成工作后解聘//需要一份工作招聘工作员工完成工作后解聘//线程池相当于以下//需要一份工作招聘工作员工完成工作后继续执行其他工作解雇//java 1.5 线程池复用,线程池(线程如何让这么多线程复用线程管理工作)//Executor// --ExecutorService// --AbstractExecutorService// --ThreadPoolExecutor//ThreadPoolExecutor 学习此类//线程池里面只有一个核心线程在跑任务/*** corePoolSize:核心线程数* maximumPoolSize线程池非核心线程数线程池规定大小* keepAliveTime:时间数值* unit时间单位* 参数三和四作用正在执行的任务Runnable 20 大于核心线程数 参数三和参数四才会起作用* 作用Runnable1执行完毕后闲置60s如果过了闲置60s会回收掉Runnable1如果在闲置时间60s内复用此线程Runnable1* workQueue队列* 作用会把超出的任务加入到队列中缓存起来*/ // ExecutorService executorService new ThreadPoolExecutor(1, 1, 60, TimeUnit.SECONDS, new LinkedBlockingDeque());//Exception in thread main java.lang.IllegalArgumentException 会崩溃 // ExecutorService executorService new ThreadPoolExecutor(5, // 1, 60, TimeUnit.SECONDS, new LinkedBlockingDeque());// ExecutorService executorService new ThreadPoolExecutor(5, // 10, 60, TimeUnit.SECONDS, new LinkedBlockingDeque());//想实现缓存线程池方案/*** corePoolSize:核心线程数* maximumPoolSize最大线程数。线程池非核心线程数线程池规定大小* keepAliveTime:时间数值* unit时间单位* 参数三和四作用正在执行的任务Runnable 20 大于核心线程数 参数三和参数四才会起作用* 作用Runnable1执行完毕后闲置60s如果过了闲置60s会回收掉Runnable1如果在闲置时间60s内复用此线程Runnable1* workQueue队列* 作用会把超出的任务加入到队列中缓存起来*/ // ExecutorService executorService new ThreadPoolExecutor(0, // Integer.MAX_VALUE, 60, TimeUnit.SECONDS, new LinkedBlockingDeque());ExecutorService executorService new ThreadPoolExecutor(0,Integer.MAX_VALUE, 60, TimeUnit.SECONDS, new SynchronousQueue(),new ThreadFactory() {Overridepublic Thread newThread(Runnable r) {Thread thread new Thread();thread.setName(MyOkHttp Dispatcher);thread.setDaemon(false);//不是守护线程return thread;}});for (int i 0; i 20; i) { //循环第二次闲置60s复用上一次任务executorService.execute(new Runnable() {Overridepublic void run() {try {Thread.sleep(1000);System.out.println(当前线程执行耗时任务线程是 Thread.currentThread().getName());} catch (InterruptedException e) {e.printStackTrace();}}});}/**************************************JAVA提供了API***********************************************///Java设计者考虑到了不用使用线程池的参数配置提供了APIExecutorService executorService1 Executors.newCachedThreadPool();executorService1.execute(new Runnable() {Overridepublic void run() {}});//线程池里面只有一个核心线程最大线程也只有一个ExecutorService executorService2 Executors.newSingleThreadExecutor();Executors.newFixedThreadPool(5); //指定固定大小线程池} } 3、守护线程详解 /*** Author: ly* Date: 2023/9/3* Description: 守护线程的使用*/ public class MyThread {public static void main(String[] args) {Thread thread new Thread() {Overridepublic void run() {super.run();while (true) { // try { // Thread.sleep(10); // } catch (Exception e) { // e.printStackTrace(); // } finally { // System.out.println(run...); // }System.out.println(run...);}}};//守护线程thread.setDaemon(true);thread.start();//JVM main()所持有的进程该结束了try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}} }4、根据OkHttp中构建者模式写一个例子 1.定义一个类HomeParam /*** Author: ly* Date: 2023/9/3* Description: 房子的图纸*/ public class HomeParam {private double width;private double height;private String color 白色;public HomeParam() {}public HomeParam(double width, double height, String color) {this.width width;this.height height;this.color color;}public double getWidth() {return width;}public void setWidth(double width) {this.width width;}public double getHeight() {return height;}public void setHeight(double height) {this.height height;}public String getColor() {return color;}public void setColor(String color) {this.color color;}Overridepublic String toString() {return 画出来的图纸HomeParam{ width width , height height , color color \ };} }2.定义一个类House /*** Author: ly* Date: 2023/9/3* Description: 真实存在的房子*/ public class House {private double width;private double height;private String color;public House() {}public House(double width, double height, String color) {this.width width;this.height height;this.color color;}public double getWidth() {return width;}public void setWidth(double width) {this.width width;}public double getHeight() {return height;}public void setHeight(double height) {this.height height;}public String getColor() {return color;}public void setColor(String color) {this.color color;}Overridepublic String toString() {return 具体建造出来的房子House{ width width , height height , color color \ };} } 3、定义一个类Worker /*** Author: ly* Date: 2023/9/3* Description: 工人开始建造房子*/ public class Worker {//拿到图纸private HomeParam mHomeParam;public void setHomeParam(HomeParam homeParam) {mHomeParam homeParam;}//工作盖房子public House buildHouse() {House house new House();house.setHeight(mHomeParam.getHeight());house.setWidth(mHomeParam.getWidth());house.setColor(mHomeParam.getColor());return house;} }4、定义一个类DesignerPerson /*** Author: ly* Date: 2023/9/3* Description: 设计师*/ public class DesignerPerson {private HomeParam mHomeParam;private Worker mWorker;public DesignerPerson() {mHomeParam new HomeParam();mWorker new Worker();}/*** 增加楼层** param height 高度*/public DesignerPerson addHeight(double height) {mHomeParam.setHeight(height);return this;}/*** 增加宽度** param width 宽度*/public DesignerPerson addWidth(double width) {mHomeParam.setWidth(width);return this;}/*** 增加颜色** param color 颜色*/public DesignerPerson addColor(String color) {mHomeParam.setColor(color);return this;}/*** 把图纸给工人* 员工说房子盖好了** return*/public House build() {mWorker.setHomeParam(mHomeParam);return mWorker.buildHouse();} }5.定义一个类UserClient /*** Author: ly* Date: 2023/9/3* Description: 用户有一个需求盖房子*/ public class UserClient { // public static void main(String[] args) {//第一版//找到建筑公司 // DesignerPerson designerPerson new DesignerPerson(); // designerPerson.addHeight(4); // designerPerson.addWidth(120.0); // designerPerson.addColor(绿色); // // designerPerson.addHeight(2); // designerPerson.addWidth(100.0); // designerPerson.addColor(红色); // // designerPerson.addHeight(3); // designerPerson.addWidth(90.0); // designerPerson.addColor(黄色); // // //复制的过程 // // House house designerPerson.build(); // System.out.println(house); // }public static void main(String[] args) {//第二版链式调用House house new DesignerPerson().addColor(白色).addWidth(100).addHeight(8).build();System.out.println(house);} } 2、OkHttp主线流程源码阅读 1.OSI七层模型,TCP/IP模型(四层),HTTP格式OSI七层参考模型 -- TCP/IP参考模型TCP/IP参考模型四层应用层 -- HTTP,HTTPS传输层 -- SocketHTTP get(请求行,请求属性集) post(请求行,请求属性集,type(form表单提交还是其他提交),len(长度)请求体)2.OkHttp源码的主线流程 OkHttp的使用OkHttpClient 通过构建者设计模式得到OkHttpClient Request 通过构建者设计模式得到Request Call 实际得到的是final class RealCall implements Call //异步方法 call.enqueue(new Callback() //不能执行大于1次 enqueue 否则会抛出异常Exception in thread main java.lang.IllegalStateException: Already Executed synchronized (this) {if (executed) throw new IllegalStateException(Already Executed);executed true;} //拿到调度器dispatcher执行enqueue()方法 client.dispatcher().enqueue(new AsyncCall(responseCallback));Dispatcher{/** Ready async calls in the order theyll be run. */ 等待队列private final DequeAsyncCall readyAsyncCalls new ArrayDeque();运行的队列/** Running asynchronous calls. Includes canceled calls that havent finished yet. */private final DequeAsyncCall runningAsyncCalls new ArrayDeque();//最终会调用到Dispatcher类中的enqueue()方法synchronized void enqueue(AsyncCall call) {//同时运行的异步任务小于64同时访问(同一个)的服务器,不能超过5个 条件满足加入到运行队列中然后执行if (runningAsyncCalls.size() maxRequests runningCallsForHost(call) maxRequestsPerHost) {runningAsyncCalls.add(call);executorService().execute(call); //执行} else {//加入到等待队列readyAsyncCalls.add(call);}}Deque双端队列Deque双端队列是一种用于管理HTTP请求和响应拦截器的数据结构。Deque是Double-ended Queue的缩写表示它可以在两端进行元素的插入和删除操作。AsyncCall 执行耗时任务signalledCallback 为true这个错误是用户造成的和OkHttp没有关系为false这个错误是OkHttp造成的。 onFailure}梳理主线流程 OkHttpClient -- Request - newCall RealCall.enqueue(){不能重复执行} -- Dispatcher.enqueue(AsyncCall)-- Dispatcher{if:先加入运行队列里面去执行异步任务 else 直接加入等待队列} -- 异步任务 AsyncCall.execute()分析OkHttp里面的线程池 executorService().execute(call);public synchronized ExecutorService executorService() {if (executorService null) {executorService new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60, TimeUnit.SECONDS,new SynchronousQueueRunnable(), Util.threadFactory(OkHttp Dispatcher, false));}return executorService; } 分析结果OkHttp里面的线程池采用的是缓存方案线程工厂 name 不是守护线程 总结采用的是缓存方案定义线程工程(设置线程名设置不是守护线程) 缓存方案参数1 0参数2 Integer.MAX_VALUE参数3 60s闲置时间只要Runnable 大于参数1 起作用(60s 之内就会复用之前的任务60s之内就会回收任务)--------------------------------- 看OkHttp源码发现OkHttp里面使用了构建者设计模式所以才要学习构建者设计模式 OkHttpClient ---构建者模式 Request ---构建者模式 开始学习构建者设计模式 --盖房子的例子(根据OkHttp源码中的链式调用优化)
http://www.dnsts.com.cn/news/105271.html

相关文章:

  • 沈阳成创网站建设公司京东商城商务网站建设目的
  • 做图模板下载网站深圳app制作开发公司排名
  • 南宁手机模板建站深圳百度网站推广
  • 怎样优化排名自己网站微信小程序官方电话
  • 商城网站建设功能点价格如何设计公司官网站
  • 企业网站设计能否以如何提高网站百度权重
  • .net电商网站开发经常投诉网站快照
  • 怎样用godaddy建设一个网站比较好的手机网站
  • 重庆智能网站建设中国医疗器械网官网
  • 贸易网站源码深圳发布稳增长措施
  • 织梦网站更新Html微信公众号平台怎么开发
  • 西宁做网站的公司做英文网站要做适合已经的
  • 花瓣网是仿国外那个网站做的企业网站的建立要做的准备
  • 全国招聘网站排名php网站制作 青岛
  • 免费外贸网站模板下载短视频代运营费用明细
  • 响应式网站首页网站改版好吗
  • 昆明做网站视频上传网站源码
  • 杭州开发区网站建设黑色大气金融投资企业网站模板
  • 网站服务器解决方案建站程序的价钱
  • 网站推广的方案设计怎么写自建网站迁移
  • 网站建设优化收费海口网站运营托管咨询
  • 宠物网站建设的可行性金华在线制作网站
  • 建设网站需要的资质证书免费素材网站排行榜
  • 网站开发对数据库的要求wordpress发的文章怎么删除
  • 建设一个外贸网站.网页布局设计器
  • 上海电子商务网站镇江公司网站建设
  • 广东专业网站建设报价广东省建设厅网站首页
  • 阿里云做网站怎么样网络规划设计师论文背别人的行么
  • 如何用pageadmin做网站代码网站怎么制作
  • 百度做网站电话多少建筑网下载