Java Reference
In-Depth Information
CHAPTER 11
Nonblocking I/O
Compared to CPUs and memory or even disks, networks are slow. A high-end modern
PC is capable of moving data between the CPU and main memory at speeds of around
six gigabytes per second. It can move data to and from disk at the much slower but still
respectable speed of about 150 megabytes per second. 1 By contrast, the theoretical max‐
imum on today's fastest local area networks tops out at about 150 megabytes per second,
though many LANs only support speeds ten to a hundred times slower than that. And
the speed across the public Internet is generally at least an order of magnitude smaller
than what you see across a LAN. My faster than average FIOS connection promises 6
megabytes per second down and 3 megabytes per second up, about 5% of what my LAN
can support. CPUs, disks, and networks are all speeding up over time. These numbers
are all substantially higher than I reported in the third edition of this topic 10 years ago.
Nonetheless, CPUs and disks are likely to remain several orders of magnitude faster
than networks for the foreseeable future. The last thing you want to do in these cir‐
cumstances is make the blazingly fast CPU wait for the (relatively) molasses-slow net‐
work.
The traditional Java solution for allowing the CPU to race ahead of the network is a
combination of buffering and multithreading. Multiple threads can generate data for
several different connections at once and store that data in buffers until the network is
actually ready to send it; this approach works well for fairly simple servers and clients
without extreme performance needs. However, the overhead of spawning multiple
threads and switching between them can be nontrivial. For instance, each thread re‐
1. These are rough, theoretical maximum numbers. Nonetheless, it's worth pointing out that I'm using megabyte
to mean 1,024*1,024 bytes and gigabyte to mean 1,024 megabytes. Manufacturers often round the size of a
gigabyte down to 1,000 megabytes and the size of a megabyte down to 1,000,000 bytes to make their numbers
sound more impressive. Furthermore, networking speeds are often referred to in kilo/mega/giga bits per
second rather than bytes per second. Here I'm reporting all numbers in bytes so I can compare hard drive,
memory, and network bandwidths.
 
Search WWH ::




Custom Search