Blocking in terms of java.io/java.nio

梦想的初衷 提交于 2019-12-02 13:12:50

'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.

'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.

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.

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. The thread can do NOTHING else in the meantime.

So blocking is to do with the thread itself, not the data source.

Kushal

Consider a situation where you have 2 threads. 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. The reason is due to Blocking I/O

  • Blocking I/O: This is I/O which waits indefinitely for availability of source. The execution of thread waits at that point and it increases chances of Hang or Slowness of your application. java.io package is example of this type

  • Non-Blocking I/O: This is I/O which will not wait for source for infinite time, but will return immediately. java.nio package is example of this type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!