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

a thread question

Status
Not open for further replies.

skarosi

Programmer
Jan 25, 2004
140
GR
Hi all...again
its the third post in just two days, but my assignment deadline is approaching and i am at the beggining.
anyhow,
i need to run 2 methods, from different classes as threads.
i have tried more or less everything that i could get my hands on, so i am asking you, my last chance;)

ok,i have this:
public class HelloClient{
public static void main(String[] args) {
TellClients tell = new TellClients();
ListenMonitors listen= new ListenMonitors();
tell.start();
listen.run(args);
}//main
}//HelloClient class

class ListenMonitors extends Thread
{
public void run(String args[]) {
try{...
...
}
catch{}
}//ListenMonitors


class TellClients extends Thread
{ public void run(String args[]){
try{
} //try
catch (Exception e) {
System.err.println("ERROR: " + e);
e.printStackTrace(System.out);
}
}//main
}//TellClients
i have tried to call run within the methods, from the first class, everything that i could thing of, but nothing, it only run the furst one.

thanks, Ilias
 
If you want to run a class as a thread you must extend Thread class (as you do) and also override a method with a no-arg signature for the run() method - which you do not - which is why it does not work.

If you want to pass in parameters to a Thread class you need to pass them into the constructor, and leave the run() method with no args.

Eg :

Code:
class ListenMonitors extends Thread
{

public ListenMonitors(String args[]) {
  this.args = args;
}

String args[] = null;

public void run() {
   try{...
       ...
       }
   catch{}
 }//ListenMonitors

--------------------------------------------------
Free Database Connection Pooling Software
 
Thanks man. that did the trick!
it seems to me that u have an answer for everybody, dont u?
:D
thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top