• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread Question on Exam

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

Which of the following is always a 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
e is correct.
Here is the explaination:


The exam might have a question that invokes the start method on the same thread twice. The correct answer will state that an exception is thrown at run-time. In reality, the JVM would probably ignore the second attempt to start the thread and would probably not throw the exception if the thread were already dead. This code example includes a lot of thread synchronization code that ensures that the thread is not dead when the start method is invoked the second time. You can run this example yourself and get the correct result. On the real exam, the synchronization code will not be included.


As it said, on the real exam, the synchronization code will not be included. So, the first thread will probably be already dead before starting the second one. Which one is correct choice? If add an option like this: "G: The result is undetemined." It's better one,right??
Thanks,
Roger
 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi:
"In reality, the JVM would probably ignore the second attempt to start the thread and would probably not throw the exception if the thread were already dead. "
I don't know how accurate this statement is. The API says "throws IllegalThreadSateException if the thread was already started." It doesn't mention anything about ignoring the Exception. Also, I don't know where in the JLS does it say the JVM will ignore the second call to <start()>
any pointers??
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.


question 16
http://www.danchisholm.net/apr18/topic/section7/threads2.html
 
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 Roger
It is because of a bug. Actually if the Thread.start() method is invoked on a same instance more than once an exception should be reported. So the fact that the JVM ignores the second attempt is actually the fault of the JVM. So the correct answer is e. An IllegalThreadStateException is thrown at run-time.
Link to the bug report.
 
Roger Zhao
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More clear for me.
1)

For the purposes of the exam, the exception is always thrown in response to the second invocation of the start method.


2)The fact that the JVM ignores the second attempt is actually the fault of the JVM. It is a bug!
Thanks a lot!!!
[ September 04, 2003: Message edited by: Roger Zhao ]
 
Their achilles heel is the noogie! Give them noogies tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic