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;

}

}

[2095 byte] By [martinelligegea] at [2007-10-2 16:21:44]
# 1
> Why I do not enter the run method ?Because you never start() your TestThread.
CeciNEstPasUnProgrammeura at 2007-7-13 17:17:38 > top of Java-index,Java Essentials,Java Programming...
# 2
Yes I do. I got the message "About to start the thread". Just before the th.start() instruction
martinelligegea at 2007-7-13 17:17:38 > top of Java-index,Java Essentials,Java Programming...
# 3

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();

aconst_nulla at 2007-7-13 17:17:38 > top of Java-index,Java Essentials,Java Programming...
# 4

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).

aconst_nulla at 2007-7-13 17:17:38 > top of Java-index,Java Essentials,Java Programming...
# 5

> 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.

CeciNEstPasUnProgrammeura at 2007-7-13 17:17:38 > top of Java-index,Java Essentials,Java Programming...
# 6
and, btw, CeciNEstPasUnProgrammeur was spot on: you never start() your TestThread.
aconst_nulla at 2007-7-13 17:17:38 > top of Java-index,Java Essentials,Java Programming...
# 7

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;

}

}

Ravichandrana at 2007-7-13 17:17:38 > top of Java-index,Java Essentials,Java Programming...
# 8

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 ?

martinelligegea at 2007-7-13 17:17:38 > top of Java-index,Java Essentials,Java Programming...
# 9

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 > top of Java-index,Java Essentials,Java Programming...
# 10
Thanks MLRon having included "this" makes things work fine now.Again a great thank.Gege
martinelligegea at 2007-7-13 17:17:38 > top of Java-index,Java Essentials,Java Programming...
# 11
You're welcome. I'm surprised no one specifically mentioned it above.
MLRona at 2007-7-13 17:17:38 > top of Java-index,Java Essentials,Java Programming...
# 12

> 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)

aconst_nulla at 2007-7-13 17:17:38 > top of Java-index,Java Essentials,Java Programming...
# 13

> > 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 > top of Java-index,Java Essentials,Java Programming...