Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Java Help

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.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-21-2006, 02:37 PM
Sionofdarkness Sionofdarkness is offline
Programming Expert
 
Join Date: Jul 2006
Posts: 385
Rep Power: 10
Sionofdarkness is on a distinguished road
Default Multi-threaded

I read somewhere that Java is "multi-threaded". What does it mean for a language to be multi-threaded? The thing that comes to mind is multiple uses, but basically all languages have multiple uses, so it can't mean that...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 07-21-2006, 04:52 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 2,057
Last Blog:
wxWidgets is NOT code ...
Rep Power: 24
WingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the rough
Default

A multi-threaded program is a program that allows different pieces of code to run at the same time. The advantage is that you can have a single program working on several things at once. The disadvantage is that you can actually have it run SLOWER than a regular program. You also introduce more potential for bugs, and it is generally harder to debug.

Right now, you should probably worry about getting comfortable with the other features of Java.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-22-2006, 11:34 AM
Sionofdarkness Sionofdarkness is offline
Programming Expert
 
Join Date: Jul 2006
Posts: 385
Rep Power: 10
Sionofdarkness is on a distinguished road
Default

So like any feature, it has it's advantages and disadvantages. It definately sounds very useful, as long as all the bugs could be worked out. Can all programs act as multi-threaded programs?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-23-2006, 09:42 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 2,057
Last Blog:
wxWidgets is NOT code ...
Rep Power: 24
WingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the rough
Default

They can, but many programs will get no benefit from being multi-threaded. One example that comes to mind is StarCraft. You could have each unit running on its own thread, but it is easier to store the units in a list and iterate down the list, updating each unit on each pass. It makes more sense to store the units in a list and just go down the list, because it's simpler to program and probably MORE efficient.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-29-2006, 12:11 PM
Sionofdarkness Sionofdarkness is offline
Programming Expert
 
Join Date: Jul 2006
Posts: 385
Rep Power: 10
Sionofdarkness is on a distinguished road
Default

Nice comparison, it seems the second time you explain things they always make more sense.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 07-29-2006, 02:47 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 2,057
Last Blog:
wxWidgets is NOT code ...
Rep Power: 24
WingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the rough
Default

Every concept you have asked about has a purpose in programming. The key is understanding what the purpose is and when to use it. Since some concepts are more complicated, they can take longer. And like most things, explanations often raise additional questions.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 07-31-2006, 07:58 PM
brackett brackett is offline
Programmer
 
Join Date: May 2006
Posts: 193
Rep Power: 10
brackett is on a distinguished road
Default

While I agree that, in a lot of cases, there's little benefit in multithreading *now* - the dual+ core revolution will soon turn that idea on it's head. The main reason multithreading doesn't benefit a lot of programs is that you're still only able to execute a single thread at once. Dual (and the coming 4 and 8 way CPUs) change that, and multithreading will become the main way to speed up an app.
Hopefully, compilers will catch up and start to multithread optimize on their own. Until that happens though, I'd suggest getting pretty comfortable with threads.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 08-08-2006, 04:53 AM
amac amac is offline
Newbie
 
Join Date: Aug 2006
Posts: 8
Rep Power: 0
amac is on a distinguished road
Default

Quote:
Originally Posted by brackett
While I agree that, in a lot of cases, there's little benefit in multithreading *now* - the dual+ core revolution will soon turn that idea on it's head. The main reason multithreading doesn't benefit a lot of programs is that you're still only able to execute a single thread at once. Dual (and the coming 4 and 8 way CPUs) change that, and multithreading will become the main way to speed up an app.
Hopefully, compilers will catch up and start to multithread optimize on their own. Until that happens though, I'd suggest getting pretty comfortable with threads.
You're confusing the issue here. Multi-threading at OS level is a seperate issue from multi-threading at process level.

At the process level its often necessary to have a program with multiple threads, even if they don't actually execute concurrently. For example, you might want to listen to console input while also performing some other computation in the background. And that is important here and *now*.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 08-16-2006, 09:56 PM
brackett brackett is offline
Programmer
 
Join Date: May 2006
Posts: 193
Rep Power: 10
brackett is on a distinguished road
Default

Quote:
Originally Posted by amac View Post
You're confusing the issue here. Multi-threading at OS level is a seperate issue from multi-threading at process level.

At the process level its often necessary to have a program with multiple threads, even if they don't actually execute concurrently. For example, you might want to listen to console input while also performing some other computation in the background. And that is important here and *now*.
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.

Due to context switches and locking on shared data, multithreading your example will likely end up running slower on a single core machine. Does that mean you shouldn't multithread? Not necessarily...performance is rarely a consideration of mine anymore. But, a lot of programmers (unfortunately) have issues designing, debugging, and maintaining multithreaded code. In the best case, you still have a whole class of new and exciting bugs and scenarios to worry about.

Now, you should spin off new threads from the UI thread for long running tasks - but that's somewhat of a special case, where blocking the UI thread has the unfortunate characteristic of blocking window updates and making your app appear hung. But, gratuitously multithreading doesn't make your code faster, doesn't make it any more maintainable, and doesn't lead to more reliable code...why do it, then?

In your example, you have a need to have a long running process that the user can cancel. You have 2 choices; break the long running process in manageable chunks and poll the input in between chunks, or set up a processing thread and an input thread that communicate with a shared data structure. The processing thread will still need to check the shared data structure to catch an abort request (or you could just kill the thread...but that's a bit extreme) and you'll need to add locking semantics to support simultaneous read/writes.

On a single core machine, I'd vote for the single thread solution. It'll likely be faster, and almost definitely be more maintainable. But, on a dual core machine, the 2 threads can be run on independent CPUs - so the multi threaded solution will be faster because the processing thread can take advantage of a dedicated cache with no context switch penalty.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 08-16-2006, 09:56 PM
brackett brackett is offline
Programmer
 
Join Date: May 2006
Posts: 193
Rep Power: 10
brackett is on a distinguished road
Default

Quote:
Originally Posted by amac View Post
You're confusing the issue here. Multi-threading at OS level is a seperate issue from multi-threading at process level.

At the process level its often necessary to have a program with multiple threads, even if they don't actually execute concurrently. For example, you might want to listen to console input while also performing some other computation in the background. And that is important here and *now*.
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.

Due to context switches and locking on shared data, multithreading your example will likely end up running slower on a single core machine. Does that mean you shouldn't multithread? Not necessarily...performance is rarely a consideration of mine anymore. But, a lot of programmers (unfortunately) have issues designing, debugging, and maintaining multithreaded code. In the best case, you still have a whole class of new and exciting bugs and scenarios to worry about.

Now, you should spin off new threads from the UI thread for long running tasks - but that's somewhat of a special case, where blocking the UI thread has the unfortunate characteristic of blocking window updates and making your app appear hung. But, gratuitously multithreading doesn't make your code faster, doesn't make it any more maintainable, and doesn't lead to more reliable code...why do it, then?

In your example, you have a need to have a long running process that the user can cancel. You have 2 choices; break the long running process in manageable chunks and poll the input in between chunks, or set up a processing thread and an input thread that communicate with a shared data structure. The processing thread will still need to check the shared data structure to catch an abort request (or you could just kill the thread...but that's a bit extreme) and you'll need to add locking semantics to support simultaneous read/writes.

On a single core machine, I'd vote for the single thread solution. It'll likely be faster, and almost definitely be more maintainable. But, on a dual core machine, the 2 threads can be run on independent CPUs - so the multi threaded solution will be faster because the processing thread can take advantage of a dedicated cache with no context switch penalty.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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


All times are GMT -5. The time now is 06:05 PM.

Contest Stats

Xav ........ 161.68
neerlin ........ 100
satrian ........ 100
delia ........ 100
chili5 ........ 70.08
morefood2001 ........ 42.41
MeTh0Dz|Reb0rn ........ 28.44
RyanTuosto ........ 20
gamiR ........ 19.64
John ........ 14.46

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 68%

Ads