Thread : Why I do not enter the run method ?
Here is my sample program. I do not really understand why it does not work. I never entered the run() method. Sure I have forgotten something, but I don't know what.
Thanks for help.
Gege
publicclass TestThreadextends Threadimplements Runnable
{
boolean stop =false;
publicstaticvoid main(String[] arg){new TestThread() ;}
public TestThread()
{
Thread thisThread = Thread.currentThread();
Thread th =new Thread();
System.out.println("About to start the thread");
th.start();
stop=false;
while(!stop)
{
try
{
thisThread.sleep(1000);// wait 1 second
}
catch (InterruptedException e ){ System.out.println("Got an error ");}
}
th =null;// Kill the thread
}
publicvoid run()
{
System.out.println("I am in the Run Method");
stop=true;
}
}
> Why I do not enter the run method ?Because you never start() your TestThread.
Yes I do. I got the message "About to start the thread". Just before the th.start() instruction
Unfortunately dont have time to go in to all the extending thread is bad, etc etc etc stuff right now... But as for why your run method isn't getting called: You are creating and starting a thread with no associated runnable (and nothing to do in run):
Thread th = new Thread();
System.out.println("About to start the thread");
th.start();
With your code as is, what I presume you want to do is start yourself:
this.start();
Also, there's not much point in doing this:
Thread thisThread = Thread.currentThread();
thisThread.sleep(1000); // wait 1 second
Thread.sleep is static - you dont need a thread instance to call it.
If you want to sleep, just do Thread.sleep(x) (or take some nytol).
> Yes I do.
No you don't.
public static void main(String[] arg) { new TestThread() ; }
> I got the message "About to start the
> thread".
That's from the constructor and is of no significance.
and, btw, CeciNEstPasUnProgrammeur was spot on: you never start() your TestThread.
Try this..
public class TestThread extends Thread implements Runnable
{
boolean stop = false;
public static void main(String[] arg) {
Thread th = new TestThread();
System.out.println("About to start the thread");
th.start();
stop=false;
while(!stop)
{
try
{
Thread.currentThread().sleep(1000); // wait 1 second
}
catch (InterruptedException e ){ System.out.println("Got an error ");}
}
th = null;// Kill the thread
}
public TestThread()
{
}
public void run()
{
System.out.println("I am in the Run Method");
stop=true;
}
}
Thanks, Mr (or Ms) Ravichandranbut I can't use the "extends" word.
In fact the code I show you, is just a sample, but in reallity, it would already inherite from a major class, and Java does not allow mutiple -inheritances.
As far as I have understood threads, I thought that implementing Runnable would resolve the need for a class tobe an extension of Thread.
Any other ideas ?
Just implement Runnable, don't extend Thread.
Change this:
Thread th = new Thread();
to this:
Thread th = new Thread(this); // start a thread with this as the Runnable
MLRona at 2007-7-13 17:17:38 >

Thanks MLRon having included "this" makes things work fine now.Again a great thank.Gege
You're welcome. I'm surprised no one specifically mentioned it above.
MLRona at 2007-7-13 17:17:38 >

> You're welcome. I'm surprised no one specifically mentioned it above.
Yeah - I only hinted at it:
"Unfortunately dont have time to go in to all the extending thread is bad, etc etc etc stuff right now"
But I did state how the OP could solve the actual problem:
"this.start();" :o)
> > You're welcome. I'm surprised no one specifically
> mentioned it above.
>
> Yeah - I only hinted at it:
> "Unfortunately dont have time to go in to all the
> extending thread is bad, etc etc etc stuff right
> now"
>
> But I did state how the OP could solve the actual
> problem:
>
> "this.start();" :o)
Yeah, I did see that. :o)
MLRona at 2007-7-13 17:17:38 >
