File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Best ways to handle multithreading" Watch "Best ways to handle multithreading" New topic

Best ways to handle multithreading

prasad yard

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 ... ...

prasad yard

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

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

Joined: Sep 28, 2004
Posts: 20531

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.


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
I agree. Here's the link:
subject: Best ways to handle multithreading
It's not a secret anymore!