wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threading question from Dan's site Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threading question from Dan Watch "Threading question from Dan New topic
Author

Threading question from Dan's site

s khosa
Ranch Hand

Joined: May 15, 2003
Posts: 72
Hi,
This is the question on Threads from Dan's site:
class A extends Thread {
public void run() {
synchronized (this) {
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 + ",");
}
}

What are the possible results 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. Compiler error.
e. Run time error.
f. None of the above.
Answer is : a
But could one of the outcomes not be (f)- None of the above becuase what is the a1.start() call starts the thread a1 first and does not reach print statement. Is it not possible?
Thanks,
Sumeer.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Sumeer
I would say that it's not posible. This is because when a1.wait() is called thread a1 is not in the runnable state. So as for this question the only other thread(main thread) should complete. Only other thread refers to the thread that is being made in this prorgram. There might also be some other threads like garbage collection thread/s that might also run.
G Nadeem
Ranch Hand

Joined: Apr 25, 2003
Posts: 48
hi,
talking from exam point of view, i think before going for 'f' we must make sure that none of the given options is valid. if a valid option is given, 'none-of-above' is inapplicable. am i right??. if not can we, select a set of options with one being 'none-of-above' too. plz give ur opinon.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
If a1 thread is started before main thread reaches print, it would be a1 the one waiting. main thread can proceed to execute the pritn statement.
Because a1 is not notified it keeps waiting forever and the program should not end to completion.


SCJP2. Please Indent your code using UBB Code
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by G Nadeem:
hi,
talking from exam point of view, i think before going for 'f' we must make sure that none of the given options is valid. if a valid option is given, 'none-of-above' is inapplicable. am i right??. if not can we, select a set of options with one being 'none-of-above' too. plz give ur opinon.

If the "none of the above" option is correct, then no other option is correct.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threading question from Dan's site
 
Similar Threads
? from Dan's exam
Dan's Thread
Another One from Threads...
one more question from Dan's questions
daemon threads