wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes code speed up Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "code speed up " Watch "code speed up " New topic
Author

code speed up

Nora mono
Greenhorn

Joined: Apr 07, 2012
Posts: 1
Dear all,

i have java code written sequentially and i need to make it parallel code
i used threads to accomplish this
but it requires more than the seq. one
then i used only two threads and divide the work between them also it takes longer time
finally i used one thread it become seq. but it still need more time !!!
after that i tried a very simple code ( just it print message) also the seq. time is much better !!!

can any one explain that to me
i need to speed up the code not makes it much longer

thank you
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18988
    
  40

noony mm wrote:Dear all,

i have java code written sequentially and i need to make it parallel code
i used threads to accomplish this
but it requires more than the seq. one
then i used only two threads and divide the work between them also it takes longer time
finally i used one thread it become seq. but it still need more time !!!
after that i tried a very simple code ( just it print message) also the seq. time is much better !!!

can any one explain that to me
i need to speed up the code not makes it much longer



It sounds like you are under the impression that threads make "code speed up". It doesn't do that.

All you are doing, by splitting a single task, into separate tasks via threads, is to enable more resources. The idea is, if there are CPU cores previously not being used, they can now be used to run these extra threads.

However.... There is extra work to split/coordinate/merge these threads. There is extra work to synchronize these threads. And if your computer don't have extra CPU cores to spare -- all you are doing is this extra work so that your threads can share the same CPU core. And if you do a poor job at splitting the task into smaller tasks, or in a way that they can't run parallel with little synchronization efficiently, you can easily make the program take longer to complete (even with extra CPU cores helping out).

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Edward Harned
Ranch Hand

Joined: Sep 19, 2005
Posts: 291

Splitting the work into segments and processing those segments in multiple threads is what Fork/Join is all about. As Henry said, if you don't have the cores to do the work then stop right here.

If each piece of work is unique, that is a component in a request then you can look at the TymeacSE open source product here:
http://www.coopsoft.com/ar/ForkJoinArticle.html

If you need to dynamically decompose the work into identical tasks, with each task running in a separate thread then you can look at the TymeacDSE open source product here:
http://www.coopsoft.com/ar/ConquerArticle.html

Ed


Ed's latest article: A Java Parallel Calamity http://coopsoft.com/ar/Calamity2Article.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: code speed up