• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

running a fixed number of threads all the time

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.




 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .......

 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic