简体   繁体   English

根据java.io/java.nio进行阻塞

[英]Blocking in terms of java.io/java.nio

I just read ... 我只是读...

Classes that work with streams are located in two packages: java.io and java.nio. 使用流的类位于两个包中:java.io和java.nio。 Classes from the former implement blocking of input/output (I/O): When bytes are being read/written by a process, they become unavailable for other threads of execution. 前者中的类实现了输入/输出(I / O)的阻塞:当进程正在读取/写入字节时,它们将不可用于其他执行线程。 The latter package offers non-blocking I/O with improved performance. 后者提供了具有改进性能的非阻塞I / O。

... and would like to understand this a bit more. ...,并希望对此有所了解。 Does blocking only impact the single relevant thread, but leave the source (ie file or database) itself unblocked, ready to be accessed by other streams? 阻塞是否只影响单个相关线程,而使源(即文件或数据库)本身不受阻塞,可以被其他流访问? Or does the blocking actually prevent the source from being accessed itself, until the current thread is done with the reading? 还是阻塞实际上阻止了源本身的访问,直到当前线程完成读取操作为止?

'Blocking' means that the I/O method you are calling blocks the calling thread until at least some data has been transferred, or until an accept or connect operation has either succeede or failed. “阻塞”是指您正在调用的I / O方法阻塞调用线程,直到至少传输了某些数据,或者接受或连接操作成功或失败为止。

'Non-blocking' means that if no data can be transferred, the I/O method returns immediately with an appropriate return value or exception, or that a connect operation proceeds in the background and can be checked for completion later. “非阻塞”表示如果无法传输任何数据,则I / O方法将立即以适当的返回值或异常返回,或者连接操作在后台进行并可以在以后检查完成。

For completeness, 'asynchronous' means that the I/O method returns immediately but the operation continues in the background, with its result available via another call in due course, or a callback. 为了完整起见,“异步”意味着I / O方法立即返回,但操作在后台继续,其结果可通过适当时候的另一个调用或回调获得。

Blocking basically refers to when a thread invokes a read() or write(), it is blocked from doing anything else until there is some data to read or the data is written. 阻塞基本上是指线程调用read()或write()时,它将被禁止执行任何其他操作,直到有一些要读取的数据或写入该数据为止。 The thread can do NOTHING else in the meantime. 同时,线程可以执行其他操作。

So blocking is to do with the thread itself, not the data source. 因此阻塞与线程本身有关,而不与数据源有关。

Consider a situation where you have 2 threads. 考虑一下您有2个线程的情况。 Both threads are reading from single socket streams. 两个线程都从单个套接字流中读取。 Here we are concerned about the source bytes which we are reading as well as we need to check in terms of Multi-threading . 在这里,我们关心的是正在读取的源字节以及我们需要检查的Multi-threading The reason is due to Blocking I/O 原因是由于Blocking I/O

  • Blocking I/O: This is I/O which waits indefinitely for availability of source. 阻止I / O:这是无限期等待源可用性的I / O。 The execution of thread waits at that point and it increases chances of Hang or Slowness of your application. 线程的执行将在此时等待,这会增加应用程序Hang或运行Slowness的可能性。 java.io package is example of this type java.io包是这种类型的示例

  • Non-Blocking I/O: This is I/O which will not wait for source for infinite time, but will return immediately. 非阻塞I / O:这是I / O,它不会无限期地等待源,但会立即返回。 java.nio package is example of this type java.nio包是这种类型的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM