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

麻涌东莞网站建设高端软件定制开发

麻涌东莞网站建设,高端软件定制开发,好看的html页面模板,响水专业做网站newCall 实际上是创建了一个 RealCall 有三个参数:OkHttpClient(通用配置,超时时间等) Request(Http请求所用到的条件,url等) 布尔变量forWebSocket(webSocket是一种应用层的交互方式,可双向交互…

在这里插入图片描述
newCall 实际上是创建了一个 RealCall 有三个参数:OkHttpClient(通用配置,超时时间等) Request(Http请求所用到的条件,url等) 布尔变量forWebSocket(webSocket是一种应用层的交互方式,可双向交互,一般用不到,除非需要频繁刷新数据,股票等。)

	private RealCall(OkHttpClient client, Request originalRequest, boolean forWebSocket) {this.client = client;this.originalRequest = originalRequest;this.forWebSocket = forWebSocket;}

Request会被进行多次封装(所以构造函数里对象被命名为originRequest)

在进行newCall().enqueue(),实际就是RealCallenqueue()

	@Override public void enqueue(Callback responseCallback) {synchronized (this) {if (executed) throw new IllegalStateException("Already Executed");executed = true;}//1transmitter.callStart();//2 关键client.dispatcher().enqueue(new AsyncCall(responseCallback));}

分别看1和2,主要看2

	public void callStart() {//跟踪程序错误this.callStackTrace = Platform.get().getStackTraceForCloseable("response.body().close()");//eventListener是一个监听器,连接的接入和关闭,对程序进行监听eventListener.callStart(call);}

client.dispatcher()返回一个Dispatcher类对象,即线程调度器,然后用这个去进行异步操作,代入参数为一个AsyncCall

	void enqueue(AsyncCall call) {synchronized (this) {//1readyAsyncCalls.add(call);// Mutate the AsyncCall so that it shares the AtomicInteger of an existing running call to// the same host.//2if (!call.get().forWebSocket) {AsyncCall existingCall = findExistingCallWithHost(call.host());if (existingCall != null) call.reuseCallsPerHostFrom(existingCall);}}//3promoteAndExecute();}
  • 1的readyAsyncCalls 是一个 ArrayDeque<AsyncCall> ,存放 准备要执行但还没有执行,然后会在3的promoteAndExecute()中执行
	private boolean promoteAndExecute() {assert (!Thread.holdsLock(this));List<AsyncCall> executableCalls = new ArrayList<>();boolean isRunning;synchronized (this) {for (Iterator<AsyncCall> i = readyAsyncCalls.iterator(); i.hasNext(); ) {AsyncCall asyncCall = i.next();if (runningAsyncCalls.size() >= maxRequests) break; // Max capacity.if (asyncCall.callsPerHost().get() >= maxRequestsPerHost) continue; // Host max capacity.i.remove();asyncCall.callsPerHost().incrementAndGet();executableCalls.add(asyncCall);runningAsyncCalls.add(asyncCall);}isRunning = runningCallsCount() > 0;}for (int i = 0, size = executableCalls.size(); i < size; i++) {AsyncCall asyncCall = executableCalls.get(i);asyncCall.executeOn(executorService());}return isRunning;}

promoteAndExecute会挑选那些不会导致超负载的call(不超过AsyncCall对应的maxRequest),放进executableCallsrunningAsyncCalls,然后去执行,就是去遍历executableCalls然后执行。
分别执行就是把调用每一个asyncCall 的 executeOn():

	void executeOn(ExecutorService executorService) {assert (!Thread.holdsLock(client.dispatcher()));boolean success = false;try {executorService.execute(this);success = true;} catch (RejectedExecutionException e) {InterruptedIOException ioException = new InterruptedIOException("executor rejected");ioException.initCause(e);transmitter.noMoreExchanges(ioException);responseCallback.onFailure(RealCall.this, ioException);} finally {if (!success) {client.dispatcher().finished(this); // This call is no longer running!}}}

核心只有一行:

executorService.execute(this);

这里就已经切换线程了,执行的都是传入的 executorService.对象 的execute()方法,都会在后台执行

	@Override protected void execute() {boolean signalledCallback = false;  // 标记回调是否已触发transmitter.timeoutEnter();  // 进入超时处理逻辑try {Response response = getResponseWithInterceptorChain();  // 调用拦截器链获取responsesignalledCallback = true;  // 标记回调已触发responseCallback.onResponse(RealCall.this, response);  // 调用response的回调函数} catch (IOException e) {if (signalledCallback) {// 不要重复触发回调!Platform.get().log(INFO, "Callback failure for " + toLoggableString(), e);} else {responseCallback.onFailure(RealCall.this, e);}} catch (Throwable t) {cancel();  // 取消请求if (!signalledCallback) {IOException canceledException = new IOException("canceled due to " + t);canceledException.addSuppressed(t);responseCallback.onFailure(RealCall.this, canceledException);  // 调用失败回调函数}throw t;} finally {client.dispatcher().finished(this);  // 请求结束,将请求移出调度队列}}

其中回调函数就是当初我们在应用层所定义的Callback里边定义的onFailure()onResponse(),然后如果出现异常,会进行调用相应的方法。

http://www.yayakq.cn/news/459334/

相关文章:

  • 网站开发好学江西吉安建设监督网站
  • hugo 怎么做网站成都网站排名 生客seo怎么样
  • wordpress 名站西宁做网站君博解决
  • 做一婚恋网站多少钱大型新型网站
  • 茶叶公司网站源码建站网站和维护需要会什么区别
  • 郑州网站建设e00番禺 大石网站建设
  • 做哪个网站的推广好网站建设地址 北京
  • 网站建设公司获得风投怎么制作网站logo
  • 建个企业网站备案需要多长时间wordpress订单系统
  • saas做视频网站网络营销策划书案例
  • 常熟网站制作找哪家好制作网页的12个步骤
  • ps做 网站标准尺寸余江区建设局网站
  • 运城 网站 建设 招聘网络舆情案例分析
  • 网站标题优化可以含几个关键词网页设计与制作教程实验报告
  • 5东莞网站建设云服务器可以用来做网站么
  • 有风险的网站网上购物系统的设计与实现论文
  • 培训网站哪个最好的网站开发技术支持与保障
  • 做快手网站hao123网站用什么程序做的
  • 什么大的网站是帝国cms做的wordpress module破解
  • 汕头网站建设stqhcx广告图片
  • 腾讯云主机能给几个网站备案手表网站背景素材
  • 基于html5的旅游网站的设计与实现网页截图快捷键可拉动
  • 聊城有什么网站制作公司xampp wordpress安装
  • 济南企业免费建站网站毕业设计选题
  • 大宗商品价格查询网站自己做网站翻译服务器 - 添加网站
  • 陵水网站建设报价怎样优化网站关键词
  • idea 做网站登录企业网站建设服务公司
  • 漯河网站关键词优化wordpress适合中文主题
  • 宜昌做网站优化海南新闻在线中心
  • 吉林省长春网站建设北京海华城市建设学校网站