IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Java 1.4 non blocking I/O apis
I think I understand the importance of non-blocking i/o but I have been reading that this will allow multiple (hundreds of connections) to be handled by 1 thread (see for example here [link|http://www.javaworld.com/javaworld/jw-09-2001/jw-0907-merlin.html|Master Merlin's new I/O classes] "Using Merlin's new I/O classes, we will build a Web server that handles thousands of connections with just three threads.") I don't understand how that can be, even if the i/o is non-blocking if I have 100 clients trying to talk to my server, how can I service them with 1 thread? My 1 thread can still only do 1 thing at a time. What am I missing here?
New Back to the future: cooperative multi-tasking
Using non-blocking IO is just the old select loop in C. You just wait in a loop that goes and checks if you can read from anyone yet, if you can then do a little processing, else go back and check again.

This has key advantages over blocking and threading. The biggest is that starting a new thread is expensive. A second one is that you get more flexibility over your control logic. A third is that by having less going on in parallel, you have fewer internal races to deal with.

This has key disadvantages as well. The biggest is that if something occupies your thread for a bit, everything comes to a halt. Another is that there are now race conditions between your noting that there is external input and your taking advantage of it. A third is that you wind up doing a classic "busy wait", occupying significant system resources in just finding out that you have nothing to do.

In reality, of course, most computers only do one thing at a time, but simulate doing many things by switching between them rapidly. (SMP computers are, of course, exceptions. But even they typically seem to be doing more things at once than they have CPUs.) All that blocking versus non-blocking really means is whether you are handling the switching logic in your code, or whether that switching is being handled at a lower level (eg in the OS).

Which is better? My belief is that, "It depends." But I would say that if you have pervasive multi-threading, then blocking IO is safer since it handles more race conditions automatically. But when given a choice, I prefer single threading to having multi-threading.

Cheers,
Ben
New Expensive thread startups...
Not really true. Both Solaris and NT keep thread pools which can be very quickly reused in this sort of situation. It's faster than a select loop. We have several programs that do exactly that, and the difference is apparent.
Regards,

-scott anderson
New That just underscores my point
Keeping pools of objects around so that you can reuse existing ones is an old optimization to avoid expensive creations. The very fact that this has been noted as a bottleneck and the optimization applied for thread creation is evidence that thread creation is expensive. Whether it works for you depends on your usage pattern, but if your process hangs around for a while, both asking for and returning objects, then the odds are very good that your usage pattern fits.

The fact that this can beat a select solution means little. As I mentioned, select has its own performance drawbacks. (Such as busy waits.) Similarly note that the performance of heavily threaded code is strongly OS dependent. It is no accident that you used NT and Solaris as your examples, but not Linux or HP-UX.

For more, a lot more, I find [link|http://www.kegel.com/c10k.html|The C10K Problem] a good read. I am fairly sure that you have seen it before, but there probably have been updates since you last read it, and there are a lot of links that are just plain good. Among other relatively recent additions are links (which I have admittedly not read) on how non-blocking I/O can be a performance win for Java.

Cheers,
Ben
New Oh look more crap from sunsoft
Must we have thousands of new classes? The current Java io library is fat enough to choke a beaver.

Howabout we just leave shit alone for a little while and call it good?

None of this crap is new or innovative and it was possible to do all this crap before so where is the value add?

OK, I feel a little better now.
New Carl Sagan meet Sunsoft: Billions and billions of classes..
"Beware of bugs in the above code; I have only proved it correct, not tried it."
-- Donald Knuth
New Yes and no
These classes do provide important functionality that was not there. The problem is that it doesn't tie well into the existing Java classes. The following quote illustrates this: [link|http://www.javaworld.com/javaworld/jw-09-2001/jw-0907-merlin.html|Master Merlin's new I/O classes]

"On the other hand, the new I/O doesn't integrate well with the old I/O. (And when exactly will it stop being new? Will Sun rename the packages when the next, newer I/O capability comes along?) In particular, the Buffers don't connect well with existing java.io classes. Just try explaining that BufferedReader can't read into Buffers and you can't pass a Buffer to BufferedWriter! The JCP operates by chartering an expert group (a committee) to address each specification request. These expert groups work independently, each with a relatively narrow charter. It is hardly surprising that there is little interaction between feature sets, or that new features do not reach broadly into the existing APIs. "

Unfortunatley this is what happens when you design a language by committees.
New Designing language
Actually, having more (enlightened) people in on the language design would have been good I think.

But the library designs have been short on vision and forethought. The "old" IO library remains a mess since the addition of Readers in place of InputStreams. Why every input class doesn't have both a Reader and InputStream ctor is beyond me - but it certainly takes some digging to come up with just the right set of nestings when you're given a InputStream and you need a type of Reader or vice versa.

Javasoft sucks at design.
     Java 1.4 non blocking I/O apis - (bluke) - (7)
         Back to the future: cooperative multi-tasking - (ben_tilly) - (2)
             Expensive thread startups... - (admin) - (1)
                 That just underscores my point - (ben_tilly)
         Oh look more crap from sunsoft - (tuberculosis) - (3)
             Carl Sagan meet Sunsoft: Billions and billions of classes.. -NT - (wharris2)
             Yes and no - (bluke) - (1)
                 Designing language - (tuberculosis)

The world of cereal is a rich and exciting one, and no mistake.
61 ms