• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Question from Dan's Thread Exam

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shekhar,
This is b'coz died threads cannot be resumed.
Some1 correct me if i am wrong.
regds
Arpana
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
sekhar variam
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Dan...Its clear now...
Sekhar
 
look! it's a bird! it's a plane! It's .... a teeny tiny ad
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic