Thread: Multi-threaded
View Single Post
  #12 (permalink)  
Old 08-21-2006, 03:24 PM
amac amac is offline
Newbie
 
Join Date: Aug 2006
Posts: 8
Credits: 0
Rep Power: 0
amac is on a distinguished road
Default

Quote:
Originally Posted by brackett View Post
I'm not sure what you mean by confusing multi-threading at OS vs process level. Threads are always a part of a process, and whether that process is owned by your application or the OS is immaterial to the discussion, I think.
I don't think it is confusing the issue at all. Firstly processes are always 'owned' by the OS. They are as high level as an application can get. Threads are a part of processes, but multi-threading often refers to what might better be described as multi-processing. Context switching doesn't take place on user-level threads (what we've been calling 'threads' so far).

The point is that kernel level threads (processes) have to perform context switches and lock on shared data, but user-level threads don't. They exist for this reason - efficiency!. So you don't get a context switching penalty, and that's a pretty major point. I'm not saying talking about multi-processing is irrelevant; just that you need to separate it from the issue of user level threads.

Writing a multi-threaded program where user input is required (as well as processing) is much, much simpler than writing a single-threaded one. Java abstracts from any complexity, and doesn't resort to mindless polling etc. How would you go about breaking "long running process in manageable chunks and poll the input in between chunks" whilst still getting all user input? It would be insane. It's not multi-threading thats inefficient - polling is.

To poll for user input wastes ridiculous amounts of cycles; using multiple threads allows the program to fall back on the OS's interrupts meaning that the thread doesn't do anything when it doesn't have to. So it's not less efficient, and I think it's important to remember that. If you poll, as you suggest, you're essentially implementing your own basic threading system - you perform some action, go off and do another, then come back to the first one. Why do that when you've got an optimised version already available? To sum up... never, never, never poll for user input (and if you do, please don't mention efficiency in the same sentence).
Reply With Quote