当前位置:网站首页>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
边栏推荐
- 論文寫作 19: 會議論文與期刊論文的區別
- selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
- R语言使用timeROC包计算存在竞争风险情况下的生存资料多时间AUC值、使用cox模型、并添加协变量、R语言使用timeROC包的plotAUCcurve函数可视化多时间生存资料的AUC曲线
- WordPress插件:WP-China-Yes解决国内访问官网慢的方法
- Cadence OrCAD capture batch change component packaging function introduction graphic tutorial and video demonstration
- [text classification cases] (4) RNN and LSTM film evaluation Tendency Classification, with tensorflow complete code attached
- Mfcc: Mel frequency cepstrum coefficient calculation of perceived frequency and actual frequency conversion
- Distinction between pointer array and array pointer
- Compact CUDA tutorial - CUDA driver API
- An error is reported when sqoop imports data from Mysql to HDFS: sqlexception in nextkeyvalue
猜你喜欢
WordPress plug-in: WP CHINA Yes solution to slow domestic access to the official website
如何在BNB鏈上創建BEP-20通證
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
Esp8266 - beginner level Chapter 1
Openharmony open source developer growth plan, looking for new open source forces that change the world!
Building googlenet neural network based on pytorch for flower recognition
如何在BNB链上创建BEP-20通证
[text classification cases] (4) RNN and LSTM film evaluation Tendency Classification, with tensorflow complete code attached
Sqoop imports tinyint type fields to boolean type
【文本分类案例】(4) RNN、LSTM 电影评价倾向分类,附TensorFlow完整代码
随机推荐
Unity 模型整体更改材质
nc基础用法2
Notes of Tang Shu's grammar class in postgraduate entrance examination English
IIS data conversion problem: 16bit to 24bit
uIP1. 0 actively sent problem understanding
Project training of Software College of Shandong University - Innovation Training - network security shooting range experimental platform (6)
Mfcc: Mel frequency cepstrum coefficient calculation of perceived frequency and actual frequency conversion
MFC obtains local IP (used more in network communication)
An error is reported in the initialization metadata of the dolphin scheduler -- it turns out that there is a special symbol in the password. "$“
redis 分布式锁
山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(七)
波场DAO新物种下场,USDD如何破局稳定币市场?
The flinkcdc reports an error: but this is no longer available on the server
WordPress插件:WP-China-Yes解决国内访问官网慢的方法
山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(八)
Alicloud: could not connect to SMTP host: SMTP 163.com, port: 25
Unity general steps for creating a hyper realistic 3D scene
Local call feign interface message 404
Understanding various team patterns in scrum patterns
Servlet learning notes