• Post Reply Bookmark Topic Watch Topic
  • New Topic

Die  RSS feed

 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
If we call the run method of the thread then, thread will die, however even though it wiil die, are we allowed to use other methods of that instance of the subclass of the thread class ?
 
amod gole
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi memati bas,
i think this example will clear your problem

class th1 extends Thread
{
int i=9;
public void run()
{
try
{
Thread.sleep(20);
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println("Thread is exititng");

}
public void disp()
{
System.out.println("Display method is called");
}
public static void main(String[] args)
{
th1 o1=new th1();
o1.start();
System.out.println("First Time Called"+o1.isAlive());
try
{
Thread.sleep(5000);
}
catch (Exception e)
{
}
System.out.println("Second Time Called"+o1.isAlive());
o1.disp();
System.out.println("Hello World!");
}
}


here we are calling disp method on same object on which run mehod is called
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One reason to reference a runnable after it has run is to get the results of an operation. Here is a very incomplete sketch of an example:

Look at the JDK 5 doc for Future and FutureTask. They does something like this ... only much better.
 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies...
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object of class Thread (or extending classes) are normal objects.

So you can call their methods any time, if you have a reference to the object.

Some methods throw an exception in some situations (e.g. start() method will throw an exception if thread is already started). But this is determined only by that method's implementation.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!