• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

running a fixed number of threads all the time

 
rajashekhar sriramoju
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
rajashekhar sriramoju
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.




 
luri ron
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .......

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic