• Post Reply Bookmark Topic Watch Topic
  • New Topic

single thread or multiple threads  RSS feed

 
manish ahuja
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
If i have a task to acheive . Which is a faster approach and Why.
Approach1) I have the whole task in 1 thread only.
or
Approach 2) i have 3 different threads doing a part of the task
Which one will be more faster & why
Rgrds
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on the nature of the task. You must satisfy these conditions.
  • The task can be divided
  • The task's parts are of sufficient size to outweigh the cost of creating each new thread
  • Most importantly, there are multiple CPUs available to run each thread/task.


  • If any of those conditions are not met, do not create multiple threads for the sake of performance improvement.
     
    Stan James
    (instanceof Sidekick)
    Ranch Hand
    Posts: 8791
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The tasks have to do something that would block the JVM. For example imagine the JVM waiting for the OS to turn a read request into a track and segment map, and for the hard drive to seek a track and read some bytes. The JVM could be running another thread during that time. Longer running things, like fetching data over a network or the Internet, give even more opportunity for multiple threads to pay off.
    But imagine a thread that's computing Pi to a zillion places. It's not giving the JVM any breaks, no chances to do other work. Splitting that task up into threads (can you?) would not gain us much.
    Starting in the late 50s my company had leading edge business computing people. Bert wrote his own language and compiler operating system mods and utilities. The Honeywell mainframe of the day had a speaker that emitted noise related to the operation of the tubes in the CPU. While reading from tape drives, the speaker went silent for seconds at a time. One of the acolytes at the temple turned to Bert and said, "Could the CPU be doing something else during that time?". Bert thought for a while, then opened his eyes and said, "Nah." They did manage to program different loops to make the speaker play Christmas carols, tho.
     
    Jim Yingst
    Wanderer
    Sheriff
    Posts: 18671
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    [CL Gilbert]: You must satisfy these conditions.
  • The task can be divided
  • The task's parts are of sufficient size to outweigh the cost of creating each new thread
  • Most importantly, there are multiple CPUs available to run each thread/task.

  • I agree with the first two; the third should be modified to include what Stan talks about. If there are multiple CPUs, or if a task will often block (e.g. waiting for user input or waitin for the hard drive to access a file) then multi-threading may well be useful. Provided the first two conditions are also true.
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12529
    48
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Stan James:
    Splitting that task up into threads (can you?) would not gain us much.

    As a side note, yes, you can split this up. There are algorithms where you can say "calculate the 1,000,000 digit of Pi, without bothering with the first 999,999. this is how they've gotten it to so many digits - distributed the task to many users, having each figure out 1000 digits or so, then put them all together.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!