Forums Register Login

starting a dead thread...

+Pie Number of slices to send: Send
Hi!
I am a very beginner in threads....
well, read in RHE that you can't restart a dead thread, I was trying it out, and I am just curious about what's happening inside when the compiler gave this error message...
public class CounterThread extends Thread
{
public void run()
{
for(int i = 0;i<10;i++)
{
System.out.println("this is it " +i);
}
}
public void invokeIt()
{
myThread.start();
}
public static void main(String [] args)
{
CounterThread myThread = new CounterThread();
myThread.start();
myThread.invokeIt();
}
}
the compiler says,unidentified variable or class name myThread .
Probably,( I assume ) since the run() of the thread has been executed and the thread has been 'done away with' there is no reference to the Thread object any more which makes the compiler say so...
Am I right,Rahul?
regards,
Vidya.

+Pie Number of slices to send: Send
oh, no !
this doesnt make any sense at all!!
public void invokeIt()
{
myThread.start();
}
sorry for being so silly...I think I should start from zero with a cup of hot coffee!!
thanx..
Vidya.
+Pie Number of slices to send: Send
CounterThread myThread is local variable to the main method that is why you are getting the error while trying to access it in other method. I have modified your code to clarify what you want



Regds.
Rahul P. Mahindrakar
+Pie Number of slices to send: Send
Thanks Rahul...I got it..
rgds,
Vidya.
Heroic work plunger man. Please allow me to introduce you to this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1520 times.
Similar Threads
Runnable confusion
Extending Thread class
Thread Doubt
Synchronization not working??
Thread memory leak
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 00:50:47.