Java Status - Blocked

I have a very simple question. If a thread is busy in I / O, why is it not being considered in the RUNNING state? If the I / O operation is time consuming, this means that the thread is doing its job. What can you call a BLOCKED stream when it really works?

+6
source share
2 answers

I do not know where you read that the thread is executing in the BLOCKED state when doing I / O. LOCKED state documentation says:

Stream status for a stream blocked while waiting for the monitor to lock. A thread in a locked state expects a monitor lock to enter a synchronized block / method or re-enter the synchronized block / method after calling Object.wait.

So, no, the thread is not in a blocking state when performing IO (unless, of course, reading or writing does not make it wait on the object’s monitor).

+4
source

If you run the following code with thread blocking on IO

public class Main { public static void main(String[] args) throws InterruptedException { final Thread thread = new Thread(new Runnable() { @Override public void run() { // blocking read try { System.in.read(); } catch (IOException e) { throw new AssertionError(e); } } }); thread.start(); for(int i=0;i<3;i++) { System.out.println("Thread status: "+thread.getState()); Thread.sleep(200); } System.exit(0); } } 

prints

 Thread status: RUNNABLE Thread status: RUNNABLE Thread status: RUNNABLE 
+4
source

Source: https://habr.com/ru/post/958083/


All Articles