In a non-blocking IO programming model, a stream blocked by available data channels, as shown below,
in python,
while True: readers, _, _ = select.select([sys.stdin, sock], [], []) # blocked select() for reader in readers: if reader is sock: print(sock.recv(1000).decode('utf-8')) else: msg = sys.stdin.readline() sock.send(msg.encode('utf-8'))
in java,
public void run() { while(true){ try{ executeCycle(); } catch(IOException e){ e.printStackTrace(); } try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } public void executeCycle() throws IOException { takeNewSockets(); readFromSockets(); writeToSockets(); }
In a non-blocking I / O programming model, the execution flow usually follows an infinite loop pattern with the primary (sole) responsibility for finding available data channels (ready) and performing I / O with an available channel.
In a scenario where all the channels are busy (for the duration), does the non-blocking I / O programming model make a thread that runs nio with / without an infinite loop also to perform another CPU-related task in the same thread, meanwhile?
source share