This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Best ways to handle multithreading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Best ways to handle multithreading" Watch "Best ways to handle multithreading" New topic
Author

Best ways to handle multithreading

prasad yard
Greenhorn

Joined: Jun 14, 2010
Posts: 3
Suppose, I am having a program that takes 1 minute to perform some calculations and having such 1000 transactions to calculate. What are the best ways to get this job done in 20 minutes? Do I have to spin 50 threads? Or are there any better ways?
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Be careful with your assumption that mulithreading will make your
operations complete sooner. Its overhead may even slow them down
since the same number of calculations must be done by the same
CPU. Have you exhausted your search for better solution algorithms?

Jim ... ...


BEE MBA PMP SCJP-6
prasad yard
Greenhorn

Joined: Jun 14, 2010
Posts: 3
Kind of, currently it is handled by multi-process application but facing time limitations as number of transactions are increased.
prasad yard
Greenhorn

Joined: Jun 14, 2010
Posts: 3
Any suggestions on this problem.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18500
    
  40


First, you need to apply common sense to the problem. Threads doesn't increase the amount of resources that you have, it just makes it possible to use it. For example, if your computer has four cores, and one thread will use one core at 100 percent, then the theoretical maximum is four times faster. And if one thread is barely using any CPU, you need to look into why -- for example, if it is IO bound on the harddisk, then increasing the number of threads just means more threads waiting for the harddisk.

So... if you want to use many threads, you need to also make sure that you have the resources to support such a program. If you don't have the CPU cycles, or the IO capacity, or the network capacity, etc., adding threads won't do anything -- except add another level of complexity.

Henry


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

Joined: Jun 08, 2010
Posts: 384

i would say ... you better program some new algo which can do your work faster rather than relying on the THREAD mechanism as sir HENRY said ...

you could kill your program alltogether ... never rely on system resources they can crash anytime ... believe me


http://plainoldjavaobject.blogspot.in
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Best ways to handle multithreading
 
Similar Threads
yet another question regarding creating a runnable jar with Eclipse/Maven
What are best ways to Learn Programming?
Your opinion # 1
How specify filter chain with @WebFilter
playing video java