|
||||||
| Java Help Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
Quote:
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). |
|
||||
|
Quote:
The discussion is, I think, when and whether multi-threading is advantageous. My argument is that it's actually *not* that much of an advantage now (because you're not "multi-processing", since 95+% of desktops are single core) but that it will become increasingly important as dual and quad cores become common place. Also, there's both a (slight) performance penalty, and a increased maintenance cost with multithreaded code. All of that needs to be considered. Quote:
Quote:
If there is no need for locking on shared data, somebody should tell the Java folks that the synchronized keyword is no longer needed. Quote:
While I have no problem with you disagreeing with me, and even pointing out that I'm a blathering idiot - I'd prefer you do it within the context of the discussion. I didn't make it a point to show that you confused "kernel-level threads" with processes, and stated that "processes" need to lock on shared data (they don't - processes communicate via IPC, and have no shared data) or that you imply that you don't need to lock on shared data within Java (a dangerous point of view, BTW). Instead, I simply replied to your arguments. I'd appreciate it if you kept the same civility. |
|
|||
|
Quote:
When you say that multi-threading is not faster on a single CPU. Not faster than what? I couldn't give you an example of a program that uses multiple threads that could be better written in a single thread; because as I said before, you resort to polling or you start rejecting input. Imagine writing a web-browser in a single thread. Aside from the size of the code-base, how would you handle connections to multiple web-pages without polling? My point is, that in single core processors, threads allow programs to abstract away the difficulty of scheduling multiple jobs. You couldn't write a faster single threaded version. So, no, multi-threading is not slower on single core CPU's (unless you mis-use them). Quote:
It's slower where it isn't needed. But in programs that require multiple connections (or similar) it isn't possible to do it more efficient any other way. |
| Sponsored Links |
|
|
|
|||||
|
Multi-Threading on a multi-core processor can give you tremendous speed advantanges, but multi-threading in any environment can allow you to perform certain tasks that just aren't as easy to make work right any other way.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |
|
|||||
|
Quote:
Traditionally, select() (combined with inline handling, or a threadpool - mainly depending on how long you'd be talking to the client) would be the most performant and scalable (actually, I/O completion ports would be better under Win32, but only because select() is poorly implemented). At a couple of hundred connections, having a single thread per connection will result in your app spending most of its time in thread management (starting, stopping, or switching). But, back to my original point. multicore CPUs start to tip the balance back towards multithreading, as you can now utilize both cores (the Apache model of using multiple processes, each handling a set of sockets with a select() call, would also work - but is probably more complicated to design). Quote:
The basic fact is that a single core CPU can only run a single thread at a time (excepting HyperThreading, which comes with its own set of rules). There's no way you can get around that. So, in effect, you're just splitting out half your logic, forcing the CPU to switch between the 2, and have to worry about shared data, locking, and synchronization (whether or not that's a big deal depends on your design and app, of course). Quote:
I'm not familiar with the internals of any web browser, but I suppose you could dig up any browser written for DOS or Win 3.1 and see a single threaded browser in action. Note that I *wouldn't* necessarily design it that way. I think that a web browser design would probably lend itself nicely to a mutlithreaded design; I actually use multiple threads quite a bit - but I am aware of the limitations and alternatives. Quote:
Quote:
It's a design choice. And, like all design choices, it's gotta be made in context. My original point was that multicore CPUs change the balance, and multithreading will likely become more common and the correct choice more often. Unfortunately, a lot of programmers do not understand the difficulties in multithreading, locking, and synchronization. Mainly because they run on a single core CPU which (since it really only runs 1 thread at a time) masks a lot of the bugs in their code. Move that to a multicore CPU, and all of those data corruption issues, deadlocking, etc. will happen a lot more often. |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How do I read .mbm (Multi BitMap) files? | moondog | General Programming | 7 | 08-07-2007 07:08 AM |
Goal: 100,000 Posts
Complete: 68%