File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes running a fixed number of threads all the time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "running a fixed number of threads all the time" Watch "running a fixed number of threads all the time" New topic
Author

running a fixed number of threads all the time

rajashekhar sriramoju
Greenhorn

Joined: Apr 15, 2003
Posts: 3
Hi

I need some help in threads.

Here is my problem -

I have to process something for hundred times using threads. I need to run 10 threads at a time. Once, I start these 10 threads, I can start another thread only if one/more of these threads are completed.

Now I need to find how I can find that a thread is available so that it can be started again. That is when a thread is completed I have to start new one otherwise, I have to wait till any of these 10 threads are completed.

Please give me some advice on this. I am using JDK 1.5.

Thanks...
Rajashekhar S


rajashekhar.s
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


The general pattern for this is to not start your own threads -- just place those 100s of tasks into a thread pool with 10 threads. The 10 threads will dequeue the tasks, as they work. Only 10 tasks will be running at any one time.

Take a look at the Executors class to see how to obtain a threadpool.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
rajashekhar sriramoju
Greenhorn

Joined: Apr 15, 2003
Posts: 3
Hi

Thanks for your quick reply. I knew the java.util.concurrent package and I am goin through newfixedThreadpool(). I dont have much documentation or examples for this. I have gone through sun's tutorial, but its not giving me much help.
Please send if you have any examples on it...

Thanks....

Rajashekhar S
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

The JavaDoc actually contains an example that is pretty much what you want....

http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ExecutorService.html

Henry
Alan Mehio
Ranch Hand

Joined: Apr 04, 2005
Posts: 73
rajashekhar sriramoju wrote:Hi

I need some help in threads.

Here is my problem -

I have to process something for hundred times using threads. I need to run 10 threads at a time. Once, I start these 10 threads, I can start another thread only if one/more of these threads are completed.

Now I need to find how I can find that a thread is available so that it can be started again. That is when a thread is completed I have to start new one otherwise, I have to wait till any of these 10 threads are completed.

Please give me some advice on this. I am using JDK 1.5.

Thanks...
Rajashekhar S


Why should you reinvent the wheel while there is a factory which give you a thread pool and this thread pool reuse the thread again ( recycle the thread).

Then if the example given is not clear, let us know.





Regards,
Alan Mehio
London, UK
luri ron
Ranch Hand

Joined: Dec 11, 2008
Posts: 87
try this
ThreadPoolExecutor pool = new ThreadPoolExecutor(10, 100, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(1));

this way if the 11 task comes in, a new thread will be created. and total threads are up to 100.
Sandeep Kumar Jakkaraju
Ranch Hand

Joined: Feb 25, 2009
Posts: 75
People used to use Thirdparty Libraries for creating the Thread Pools etc .....
Now with Java 1.5 Concurrency Utils we can use the JDK classes .............
I am still new to 1.5 but I like this .......

 
jQuery in Action, 2nd edition
 
subject: running a fixed number of threads all the time