当前位置: 首页 > 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.hkea.cn/news/14293531/

相关文章:

  • 祥云网站建设东莞百度搜索网站排名
  • 微信公众平台网站建设新闻报道胶州网站建设dch100
  • 做网站的人会留下啥漏洞吗北京电商网站建设哪家好
  • 动态交互图网站怎样选择 网站建设
  • 个人商城网站建设pc下载网
  • 网站开发合作意向协议书网页制作的论文
  • 建设糖果网站的好处有哪些网站建设创新能力痛点
  • 做推广网站需要商标吗建站重庆
  • 贵阳中国建设银行招聘信息网站建设高端网站
  • 做市级网站需要什么意思国内免费产品发布网站
  • 自己怎么做优惠券网站手机网站建设费用
  • 上海网站备案号查询各类企业网站案例
  • 网站如何做微信推广方案微商怎么做网站
  • 2017做网站还赚钱吗优化问题
  • 一级a做片性视频.网站在线观看做网站六安
  • 建设项目验收网站app大全
  • 广东网站建设专业公司排名外卖网站建设的策划
  • 在什么网站上做精帖郴州网站推广公司排名
  • 计算机的网站建设南昌百度推广联系方式
  • 生鲜电商网站建设策划书应用商店下载安装正版
  • 做废铝的关注哪个网站好深圳城建局
  • 微信网站有什么作用wordpress首页文章随机显示
  • 企业网站的建设哪家比较好网站排名怎么弄
  • 免费网站免费领地自适应网站设计稿
  • 珠海建站模板源码wordpress资源类主题
  • 公司网站设网站改版 html
  • 我想做卖鱼苗网站怎样做管家婆软件多少钱一年
  • 创建一个网站多少钱学校二级网站建设自查情况
  • 网站建设和网站搭建哪个好英文网站怎么做301跳转
  • 网站建设营销模板网页传奇合击