Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

? from Dan's exam

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually 2 questions.....
1)
class A extends Thread {
private boolean done;
public void setDone(boolean done) {this.done = done;}
public void run() {
synchronized (this) {
while (!done) {
try {wait();} catch (InterruptedException ie){}
}}}
public static void main(String[] args) {
A a1 = new A();
long startTime = System.currentTimeMillis();
a1.start();
System.out.print(System.currentTimeMillis() - startTime);
}}
Which is a possible result of attempting to compile and run the program?
a. The number printed is greater than or equal to 0
b. The synchronized block inside the run method is not necessary
c. This program runs to completion after the elapsed time is printed
d. Compile-time error
e. Run-time error
f. None of the above
the given answer is a, Howz that possible, but he has given the explanation which I had thought earlier during the exam "The notify method is never invoked on thread a1; so it will sleep forever and the program will not complete normally".
was the answer not F ??
2) A timeout of zero will allow Thread.join to wait forever if necessary
Is that "necessary" that he refers above is the state of interruption that may occur ?
~ Shalini
[ January 08, 2004: Message edited by: Taurean Lord ]
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Taurean,
Thank you for your question. I can see that I need to improve that answer explanation. The main thread invokes the start method on a1 and then invokes the print statement to print a result that is greater than or equal to zero. Thread a1 never runs to completion because the boolean variable "done" is never set to true and the notify method is never invoked on a1.
 
Oh, sure, you could do that. Or you could eat some pie. While reading this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic