Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

running a fixed number of threads all the time  RSS feed

 
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
 
author
Sheriff
Posts: 23587
138
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
Sheriff
Posts: 23587
138
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
 
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.




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

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!