• Post Reply Bookmark Topic Watch Topic
  • New Topic

starting a dead thread...

 
vidya
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
vidya
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
vidya
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rahul...I got it..
rgds,
Vidya.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!