Calling Thread run() again
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Phuong_Vo
Posted On:   Thursday, August 23, 2001 02:36 PM

After I call start() on a thread, its run() will be invoked later. What happen if while the run() is running, its run() is called again. For example, MyThread extends from Thread and overrides the run(). run() { if (nonstop == true) while (1) { System.out.println("running"); } } Below is the code that uses MyThread MyThread th = new new MyThread() nonstop = true; th.start(); sleep(5000); nonstop == false; th.run(); Will the thread dead after this? since this run() finishes its execution. What happen to the run() that has an inifinite loop after the call to start() above? Than   More>>

After I call start() on a thread, its run() will be invoked later. What happen if while the run() is running, its run() is called again.

For example,

MyThread extends from Thread and overrides the run().
run()
{
if (nonstop == true)
while (1)
{
System.out.println("running");
}
}

Below is the code that uses MyThread

MyThread th = new new MyThread()
nonstop = true;
th.start();
sleep(5000);
nonstop == false;
th.run();

Will the thread dead after this? since this run() finishes its execution. What happen to the run() that has an inifinite loop after the call to start() above?

Thank you greatly!

   <<Less

Re: Calling Thread run() again

Posted By:   Anonymous  
Posted On:   Thursday, August 23, 2001 05:15 PM

Few comments:-

1) your code will never compile because of while (1)


After you have modify your code,


2) run() will definitely execute the second time but this time, it is running under the context of the main thread.

To prove this:- modify your statement in the run() to show which thread is running under.

System.out.println (Thread.currentThread()+" is running");


3) your code is buggy, instead of the first thread (MyThread) stops executing, there will be two run being executed (modify 1 and retest).

The reason: your while (true) will never break up from the loop.


Having said all these, it would take you just 30 seconds to find out all the above. Next time, do take the time to experiment! :-)

About | Sitemap | Contact