File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads" Watch "Threads" New topic
Author

Threads

GM Shobha
Greenhorn

Joined: Apr 22, 2003
Posts: 10
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

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Apr 22, 2003
Posts: 10
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

Joined: Mar 05, 2003
Posts: 1391
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads
 
Similar Threads
Threads Again
thread start in k&b
Unexpected result with Threads join() method...
Thread.sleep() guaranteed behaviour
join, a Thread question