• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Dan's mock exam - Chap 9, Exam 2, Q18

 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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();
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 to this question is e with the following explanation:


For the purposes of the exam, invoking the start method on a thread that has already been started will generate an IllegalThreadStateException. If the start method is invoked a second time when the thread is already running then an illegal thread state exception will probably be thrown. However, if the thread is already dead then the second attempt to start the thread will probably be ignored and no exception will be thrown. However, for the purposes of the exam, the exception is always thrown in response to the second invocation of the start method. This is a case where the exam tests your knowledge of the specification of the Thread.start method and ignores the actual behavior of the 1.4 version of the JVM.


To be honest, I find the answer and explanation unsettling. How do we know that this question is about our knowledge of the specification of a certain method, and not the actual behaviour of the JVM?
Supposing you are presented with a code just like below and asked, "What wil be the output of the program?"

Although 99% of the time A will finish first, there is no guarantee. So the answer could either be "AB" or "BA".
So how is this question different from Dan's?
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


So how is this question different from Dan's?


Yes, we never know which thread the scheduler will pick first.
I'm not quite sure if I understood your point Alton, but the difference with the code you posted is that you are starting two different threads (r1.start(); r2.start() ;) , whereas in dan's exam you're starting a thread twice, which throws an exception.
did I get your question?
[ July 06, 2003: Message edited by: Andres Gonzalez ]
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andres,


However, if the thread is already dead then the second attempt to start the thread will probably be ignored and no exception will be thrown.


If you look at the explanation, you would notice that the answer could very well be a & c.
What Dan is saying is that although the answer e is no guarantee that the JVM will always behave that way in this case, it is still the right answer because the question is testing your knowledge of the specification of the Thread.start method.
For my sample code, I am pointing out that although 99% of the time the output is "AB", yet our answers are not based on highest probability, which I think his answer is based on.
[ July 06, 2003: Message edited by: Alton Hernandez ]
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alton
Whenever you see that a thread is started twice then it will throw an exception atleast in theory. But in practice you will see that it doesn't, this is because of a bug. So

will cause a compiler error even if the thread is finished or not in theory. Probably later versions will resolve this.
Again Answer e is correct because I guess that the exam wants you to know,how should the compiler behave rather than how it behaves because of a bug.
[ July 06, 2003: Message edited by: Anupam Sinha ]
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anupam Sinha:
Whenever you see that a thread is started twice then it will throw an exception at least in theory.


Ok, I got what you're saying. start() was designed to be invoked only once and should throw an exception otherwise.
Thanks.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I actually ran the code about 10 times and it threw the IllegalState exception except once when it ran fine.
That is, the 1st thread completed before the next start() was executed.
 
Run away! Run away! Here, take this tiny ad with you:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic