Skip to Main Content
Java I/O, 2nd Edition
book

Java I/O, 2nd Edition

by Elliotte Rusty Harold
May 2006
Beginner to intermediate content levelBeginner to intermediate
726 pages
19h 57m
English
O'Reilly Media, Inc.
Content preview from Java I/O, 2nd Edition

Chapter 16. Nonblocking I/O

Nonblocking I/O is one of the most important features of the new I/O API. Traditional stream-based I/O is limited by the speed of whatever it is you’re reading or writing: the disk, the network, and so on. Very often code has to sit and wait for the disk or network to respond. This is especially problematic if the program has many different things to do. For instance, a network server may need to service hundreds or thousands of simultaneous clients. It doesn’t want one slow connection to hold back all the others.

The traditional approach to this problem is to place each connection in a separate thread. One hundred simultaneous connections require one hundred threads. However, although threads are lighter-weight than processes, they still have nontrivial overhead. It takes time to set up and tear down each thread, and each one uses a finite quantity of system resources. For instance, in some versions of Windows, each thread has a megabyte of stack space. Thus, if you try to spawn 2,000 threads on a system with only a gigabyte of memory, you’re going to start swapping pretty badly (and that’s not even accounting for all the memory that’s needed for anything other than thread stacks). Thread pools improve the situation, perhaps allowing you to handle twice as many simultaneous connections as you could otherwise, but not to the point where you can simply ignore thread overhead.

Fortunately, the new buffer- and channel-based I/O comes to the rescue. In addition ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java I/O

Java I/O

Elliotte Rusty Harold
Java NIO

Java NIO

Ron Hitchens

Publisher Resources

ISBN: 0596527500Errata PageSupplemental Content