aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question from Dan's Thread Exam 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 "Question from Dan Watch "Question from Dan New topic
Author

Question from Dan's Thread Exam

sekhar variam
Greenhorn

Joined: Nov 09, 2001
Posts: 15
Hi,
I have a doubt from Dan's thread exam..
class A extends Thread {
public void run() {
System.out.print("A");
}
}
class B {
public static void main (String[] args) {
A a = new A();
a.start();
try {
a.join();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
a.start(); // 1
}
}
What is the result of attempting to compile and run the program?
a. The program compiles and runs without error.
b. The second attempt to start thread t1 is successful.
c. The second attempt to start thread t1 is ignored.
d. Compiler error at marker 1.
e. An IllegalThreadStateException is thrown at run-time.
f. None of the above

The answer is e) bcos we r trying to start a running thread...
But by calling a.join() aren't we ensuring that the thread is already dead before starting it...In that case shdn't the answer be a)
Thanks,
Sekhar..


SCJP4(96%)
Arpana Rai
Ranch Hand

Joined: Nov 12, 2002
Posts: 93
Shekhar,
This is b'coz died threads cannot be resumed.
Some1 correct me if i am wrong.
regds
Arpana


SCJP1.4(91%)
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by sekhar variam:

But by calling a.join() aren't we ensuring that the thread is already dead before starting it...In that case shdn't the answer be a)
Thanks,
Sekhar..

The exam is always looking for the answer that matches the Java Language Specification or the Javadoc on the class. When the JLS or the Javadoc disagree with the actual behavior, then the JLS or Javadoc is preferred over actual behavior.
In this case, the join method ensures the death of the thread before the second attempt to start the thread. The actual result is that the second attempt to start the thread is ignored without error. However, the Javadoc states that an IllegalThreadStateException is thrown if the thread was already started. For the purposes of the exam, just assume that a second attempt to start a thread will always result in an exception. The purpose of this question is to stress the point that we must assume an exception is thrown even though we know that it won't be--at least not on the platform that I'm running.
The emphasis on the specification over actual behavior is based on the fact that the actual behavior might someday become more consistent with the specification. Therefore, code should not be written based on an assumption that the actual behavior has become a defacto standard.
In this case, one could argue that the Javadoc is not clear and does not disallow the possibility that the exception is not thrown. On the other hand, one could also argue that the Javadoc does not guarantee that the exception won't be thrown. Based on the ambiguity we should just assume that we can not start a thread twice without risking problems.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
sekhar variam
Greenhorn

Joined: Nov 09, 2001
Posts: 15
Thanks a lot Dan...Its clear now...
Sekhar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question from Dan's Thread Exam
 
Similar Threads
Dan's mock exam - Chap 9, Exam 2, Q18
Question about Dan's Exam (Thread)
Dan's thread
Threads
dan exam doubt 21