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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Thread question

Status
Not open for further replies.

Outy

Programmer
Feb 7, 2001
16
BE
Hi,

I have to make an application that starts several threats and they all have to "listen" for another event. I have a class "Test.class", in this class I have made a vector and for each element of the vector I have to start a thread.

class Test{
public static void main(String args[])
{
Vector v = new Vector();
Enumeration e = v.elements();
v.addElement(new Mon("mon1","..."));
v.addElement(new Mon("mon2","..."));
while(e.hasMoreElements())
((Mon)(e.nextElement())).startThreads();
}
}
The class Mon contains the method startThreads()

class Mon{
private String MonName;
private String MonInfo;

public void startThreads()
{
...
}

public void run()
{

}
The problem is I don't know how to call the method run for each element of the vector from the method startThreads..
I know I have to call the runmethod with a "start" function but I tried mon.start() but it didn't work..

Help will be appreciated

Outliner
 
Each thread is started that way - and it is the job of whatever is implementing the Runnable interface to provide run() - you can add clauses, asking which is the current thread, and specifying instructions accordingly.

So don't forget to stipulate implements Runnable in class Mon.
And for each thread, you need to create a new Thread, then call it's start() instance method, so in your setup you could go thru the enumeration, making new threads for each element - put them in an array say, then when you want to start the all, run thru the array, calling the start()'s.
b2 - benbiddington@surf4nix.com
 
Make the class Mon to either :
class Mon implements Runnable{}
or
class Mon extends Thread.
Thats it.
SwapSawe.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top