Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Subtract element from Array.

Status
Not open for further replies.

Mike2020

Programmer
Mar 18, 2002
55
0
0
US

Hi All,

I created an Array.

Thread[] ThreadArray = New Thread[5];

for(int i; i <5; i++)
{
ThreaArray = new TryThread(TName, "Work ", 1000L);
ThreadArray.start();
}

now, I want the Main Thread to Check whether the Child Thread is alive or not, if it die. then Stop check it. That mean I want to remove that thread element out of the array.
How can I do that?

for(int i; i < ThreadArray.length; i++)
{
if (ThreadArray.IsAlive() == false)
{ Remove Element from array; }

}

Thanks....

Mike
 
Arrays are immutable objects because you request a specific amount of memory in the heap to store your array and its elements ...

If you want an *array* that grows or shrinks, then use an ArrayList or Vector or something :

Code:
int numThreads = 5;
Vector v = new Vector();

for(int i = 0; i < numThreads; i++) {
   Thread t = new TryThread(TName, "Work ", 1000L);
   v.addElement(t);
   t.start();
}

for(int i = 0; i < v.size(); i++) {
   Thread t = (Thread)v.elementAt(i);
   if (t.IsAlive() == false) {
       al.remove(t);
       // decrement 'i' else you will screw the vector
       i--;
   }
}

--------------------------------------------------
Free Database Connection Pooling Software
 

Hi sedj,

Thank you for your help. I did modify your suggestion, and it work.

Thanks again

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top