当前位置:网站首页>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
边栏推荐
- Tencent Qiu Dongyang: techniques and ways of accelerating deep model reasoning
- WordPress plug-in: WP CHINA Yes solution to slow domestic access to the official website
- Database query - course selection system
- Physical meaning of FFT: 1024 point FFT is 1024 real numbers. The actual input to FFT is 1024 complex numbers (imaginary part is 0), and the output is also 1024 complex numbers. The effective data is
- Alicloud: could not connect to SMTP host: SMTP 163.com, port: 25
- An error is reported in the initialization metadata of the dolphin scheduler -- it turns out that there is a special symbol in the password. "$“
- nc基础用法1
- Why is the hexadecimal printf output of C language sometimes with 0xff and sometimes not
- 基于pytorch搭建GoogleNet神经网络用于花类识别
- Understanding various team patterns in scrum patterns
猜你喜欢

【数值预测案例】(3) LSTM 时间序列电量预测,附Tensorflow完整代码

Wave field Dao new species end up, how does usdd break the situation and stabilize the currency market?

山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(八)

MySQL advanced lock - overview of MySQL locks and classification of MySQL locks: global lock (data backup), table level lock (table shared read lock, table exclusive write lock, metadata lock and inte

Physical meaning of FFT: 1024 point FFT is 1024 real numbers. The actual input to FFT is 1024 complex numbers (imaginary part is 0), and the output is also 1024 complex numbers. The effective data is

DTMF dual tone multi frequency signal simulation demonstration system

Kubernetes introduction to mastery - ktconnect (full name: kubernetes toolkit connect) is a small tool based on kubernetes environment to improve the efficiency of local test joint debugging.

Five minutes to show you what JWT is

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

Mysql database backup scheme
随机推荐
Esp8266 - beginner level Chapter 1
MFC obtains local IP (used more in network communication)
山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(七)
MySQL数据库 - 连接查询
Kubernetes getting started to proficient - install openelb on kubernetes
Introduction to electron Tutorial 4 - switching application topics
IIS data conversion problem: 16bit to 24bit
论文写作 19: 会议论文与期刊论文的区别
uIP1. 0 actively sent problem understanding
Comment créer un pass BEP - 20 sur la chaîne BNB
腾讯邱东洋:深度模型推理加速的术与道
How about Bohai futures. Is it safe to open futures accounts?
R语言使用econocharts包创建微观经济或宏观经济图、indifference函数可视化无差异曲线、自定义计算交叉点、自定义配置indifference函数的参数丰富可视化效果
Error reported by Azkaban: Azkaban jobExecutor. utils. process. ProcessFailureException: Process exited with code 64
使用 WPAD/PAC 和 JScript在win11中进行远程代码执行
Compact CUDA tutorial - CUDA driver API
Unity 模型整体更改材质
Tencent Qiu Dongyang: techniques and ways of accelerating deep model reasoning
nc基础用法3
Mysql database - single table query (II)