• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads

 
GM Shobha
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could somebody pl explain me why this code is not throwing runtime exception when i am trying to invoke start() on dead thread and also when getName() method is called on dead thread it is not responding as we can call methods on the dead thread instance except start().
class NameRunnable implements Runnable{
public void run(){
System.out.println("NameR running");
System.out.println("run by" +
Thread.currentThread().getName());
}
}
public class NameThread{
public static void main(String args[]){
NameRunnable nr=new NameRunnable();
Thread t=new Thread(nr);
t.setName("Fred");
t.start();
try{
Thread.sleep(1000);}
catch(InterruptedException e){
e.printStackTrace();}
System.out.println("main thread complets");
t.getName();
t.start();
}
}
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could somebody pl explain me why this code is not throwing runtime exception when i am trying to invoke start() on dead thread and also when getName() method is called on dead thread it is not responding as we can call methods on the dead thread instance except start().
(1) To see the name of the dead thread after the main thread wakes up, change t.getName() to System.out.println(t.getName());
(2) If you add another t.start() just after the first one, you will get an IllegalThreadStateException. If you wait until the thread returned from the run() method, you will not get the exception. This is a bug.
The same question was asked on the Java Developer Connection forums:
http://forum.java.sun.com/thread.jsp?forum=52&thread=94758
Here is the bug report:
http://developer.java.sun.com/developer/bugParade/bugs/4180576.html
If you don�t have a logon to the bug database, here are the highlights:
Synopsis:Thread.start() doesn't throw IllegalThreadStateException
State: In progress, bug
Submit Date: Oct 12, 1998
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you are really interested in the bug report, here are some more details:

[ April 22, 2003: Message edited by: Marlene Miller ]
 
GM Shobha
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.I would like to know whether such questions appear in the exam(scjp 1.4)especially the ones which are identified as bugs.
Thank you once again
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don�t know what happens on the real exam. My opinion is � I would not be surprised to see a question on the exam about restarting a thread.
That sentence might by misleading for some people whose mother tongue is other than English. What I mean is, I think it is reasonable to see a question on the exam about restarting a thread.
[ April 22, 2003: Message edited by: Marlene Miller ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic