当前位置:网站首页>Confusion about thread blocking after calling the read () method of wrapper flow
Confusion about thread blocking after calling the read () method of wrapper flow
2022-04-23 20:14:00 【nobody234】
About calling wrapper flow read() The confusion of thread blocking after method
Everyone should have used Java Of socket Well , We usually get a socket after , Will use a wrapper flow to call read() or write() Method to complete IO, The form is similar to this
// Here we use DataInputStream packing Socket Node flow of
Socket socket = new Socket(host,port);
DataInputStream dis = new DataInputStream(socket.getInputStream());
String str = dis.read();
On the opposite end socket Without sending a message , This process will be blocked in the fourth sentence , This makes me very confused , Why does a method of packing flow have blocking effect .
So I clicked on DataInputStream Class read() Method , It's like this inside
public final int read(byte[] b, int off, int len) throws IOException {
return this.in.read(b, off, len);
}
Among them the in Be being DataInputStream Wrapped node flow , In other words, he actually calls the node flow read() Method .
So here comes the question , How does this node flow block threads , This node flow is through socket and get When we arrive, let's open socket Take a look .
private volatile InputStream in;
private static class SocketInputStream extends InputStream {
private final Socket parent;
private final InputStream in;
SocketInputStream(Socket parent, InputStream in) {
this.parent = parent;
this.in = in;
}
public int read() throws IOException {
byte[] a = new byte[1];
int n = this.read(a, 0, 1);
return n > 0 ? a[0] & 255 : -1;
}
public int read(byte[] b, int off, int len) throws IOException {
return this.in.read(b, off, len);
}
public int available() throws IOException {
return this.in.available();
}
public void close() throws IOException {
this.parent.close();
}
}
original socket Node flow in , yes socket An inner class in inherits InputStream Interface implemented .
I can see it here , We call DataInputStream Of read() Method is actually called socket() An internal self implemented read Method . In other words, this thread is actually socket Obstructed , Analysis complete .
版权声明
本文为[nobody234]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210555176792.html
边栏推荐
- 如何做产品创新?——产品创新方法论探索一
- Still using listview? Use animatedlist to make list elements move
- 【文本分类案例】(4) RNN、LSTM 电影评价倾向分类,附TensorFlow完整代码
- redis 分布式锁
- STM32 Basics
- Use test of FFT and IFFT library functions of TI DSP
- 论文写作 19: 会议论文与期刊论文的区别
- nc基础用法2
- How about CICC fortune? Is it safe to open an account
- R语言ggplot2可视化分面图(facet_wrap)、使用lineheight参数自定义设置分面图标签栏(灰色标签栏)的高度
猜你喜欢

antd dropdown + modal + textarea导致的textarea光标不可被键盘控制问题

Servlet learning notes

STM32基础知识
![[numerical prediction case] (3) LSTM time series electricity quantity prediction, with tensorflow complete code attached](/img/73/ba9fb872aa279405204c411c18f348.png)
[numerical prediction case] (3) LSTM time series electricity quantity prediction, with tensorflow complete code attached

考研英语唐叔的语法课笔记

LeetCode异或运算

An error is reported when sqoop imports data from Mysql to HDFS: sqlexception in nextkeyvalue

Leetcode XOR operation

【文本分类案例】(4) RNN、LSTM 电影评价倾向分类,附TensorFlow完整代码

Distinction between pointer array and array pointer
随机推荐
R language uses timeroc package to calculate the multi time AUC value of survival data under competitive risk, uses Cox model and adds covariates, and R language uses the plotauccurve function of time
R语言ggplot2可视化分面图(facet_wrap)、使用lineheight参数自定义设置分面图标签栏(灰色标签栏)的高度
R language ggplot2 visualization: ggplot2 visualizes the scatter diagram and uses geom_ mark_ The ellipse function adds ellipses around data points of data clusters or data groups for annotation
使用 WPAD/PAC 和 JScript在win11中进行远程代码执行
How about Bohai futures. Is it safe to open futures accounts?
aqs的学习
MySQL数据库 - 连接查询
nc基础用法1
Notes of Tang Shu's grammar class in postgraduate entrance examination English
NC basic usage 2
LeetCode异或运算
NC basic usage 1
【目标跟踪】基于帧差法结合卡尔曼滤波实现行人姿态识别附matlab代码
Building googlenet neural network based on pytorch for flower recognition
MySQL数据库 - 数据库和表的基本操作(二)
NC basic usage 3
MySQL数据库 - 单表查询(二)
CVPR 2022 | querydet: use cascaded sparse query to accelerate small target detection under high resolution
Mysql database - basic operation of database and table (II)
Error reported by Azkaban: Azkaban jobExecutor. utils. process. ProcessFailureException: Process exited with code 64