Home arrow static arrow Java Programming [Archive] - *IMPORTANT* Limiting the number of active threads
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - *IMPORTANT* Limiting the number of active threads
This topic has 3 replies on 1 page.

Posts:1
Registered: 8/8/04
*IMPORTANT* Limiting the number of active threads  
Aug 8, 2004 2:45 AM



 
Hi All,

i have a few of questions so please bare with me:

1) What is the best way to limit the number of active threads? ie i want to run 10 threads but i want there to be only 3 running at a time.

this is how im thinking of doing it rite now:
count = 0;
while(...)
{
thread[count].start();
while(threadgroup.activeCount > 4){}
count++;
}

2) I have a class that extends thread how do i overide the constructor so that i can add the thread to the threadgroup?

ok here is an example of what i mean:

u can do this with a normal thread: Thread one = new Thread(threadgroup, "what");

this is what i need to do tho:

class whatever extends Thread {
public whatever(int a, int b, ThreadGroup tg)
{ what do i put here to make this thread part of the tg threadgroup?}
}

thanx for any help in advance and i kinda need a quick answer cause this is due soon , thanx youssefe2k4
 

Posts:18,384
Registered: 21.03.00
Re: *IMPORTANT* Limiting the number of active threads  
Aug 8, 2004 2:56 AM (reply 1 of 3)



 
Hi,

1) Why do you want to do that?

2) super(tg, "a name");

/Kaj
 

Posts:1,135
Registered: 1/16/04
Re: *IMPORTANT* Limiting the number of active threads  
Aug 8, 2004 3:07 AM (reply 2 of 3)



 
Hi,

1) Why do you want to do that?

2) super(tg, "a name");

/Kaj

It's part of a homework assignment... I saw another post on this forum and one at JavaRanch where you need to create an application that given a directory will scan each file and report the file name and how many lines are in them... but it must be multithreaded, each thread scan a single file and updates some list and finally you can only have three of these file scanning threads running at the same time...

Quote "To avoid having too many threads competing for the system resources when there are a lot of files in the directory, we will restrict the number of active Producer threads (active threads are started threads that have not ended yet) during the run time. For this assignment, at any time there should not be more than 3 active Producer threads. If there are already three active Producer threads during the run time, the program should wait until one of them ends to start the next Producer thread."

I was think about working on it, so that I can strengthen my knowledge in threads...
I still might do it, just for fun... I don't know yet...

We will see...

- MaxxDmg...

- ' He who never sleeps... '

 

Posts:11,186
Registered: 06.04.04
Re: *IMPORTANT* Limiting the number of active threads  
Aug 8, 2004 3:36 AM (reply 3 of 3)



 
1) What is the best way to limit the number of active threads? ie i want to run 10 threads but i want there
to be only 3 running at a time.

Use a semaphore with an initial value equal to 3. The semaphore has two synchronized methods,
P() and V() (those are Dijkstra's original names for those methods). The P() method increments
the semaphore value and notifies all (if any) waiting threads. The V() method attempts to decrement
the semaphore value but falls asleep (i.e. it waits) as long as the semaphore value equals zero.

The actual work of the threads is embraced by an initial V() call and ending with a trailing P() call.

kind regards,

Jos
 
This topic has 3 replies on 1 page.