| Author |
Thread Question - Examlab Practice Exam 1
|
Simran Dass
Ranch Hand
Joined: Jan 09, 2010
Posts: 183
|
|
The result is "ABC". Shouldn't it be "Result is unpredictable" because the output can
be either "XYZ" or "ABC".
|
 |
Jim Hoglund
Ranch Hand
Joined: Jan 09, 2008
Posts: 525
|
|
Commenting out the sleep() seems to give 'XYZ' the
edge. Of course, it depends on the scheduler, but it's
hard to see it waiting for 1-second before launching
the xmap run() method.
Jim ... ...
|
BEE MBA PMP SCJP-6
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
Try this also........
|
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
|
 |
Harpreet Singh janda
Ranch Hand
Joined: Jan 14, 2010
Posts: 317
|
|
Yes, It should be unpredictable, but if the possible answer is in the list then we should choose the possible answer
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9191
|
|
Harpreet Singh janda wrote:Yes, It should be unpredictable, but if the possible answer is in the list then we should choose the possible answer
No, if the output is unpredictable, then the correct answer is unpredictable. The questions in the exam tell you how many options to select, so if it says select 1 option, and the options contain Output "ABC", Output "XYZ" and Output is unpredictable, then the correct answer is Output is unpredictable...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
|
Thanks Ankit....
|
 |
Rajeev Trikha
Ranch Hand
Joined: Jan 29, 2010
Posts: 85
|
|
|
I think there is more information in the question then you have disclosed. Vaguely from memory it states that make the assumption that run took no time to execute. This extra info justified the given answer.
|
Rajeev Trikha (SCJP 6)
|
 |
Simran Dass
Ranch Hand
Joined: Jan 09, 2010
Posts: 183
|
|
BUT IF THE OPTIONS ARE JUST
- ABC
- XYZ
What will be the answer. Please give the explanation. I ran the program and found :-
When the line Thread.sleep() is commented the output is sometimes ABC , at other times XYZ. BUT when its NOT commented the output
is ALWAYS "ABC". Cannot understand why its always ABC , shouldn't it be XYZ because as main thread has acquired a lock on object td so even if td.start() is executed the
run() method cannot get into synchronized(this) as the lock is already acquired by main() method. And by the time main() comes out of synchronized(td) block value of s has been set
to "XYZ". So run() will have to print "XYZ" only , how come it prints "ABC"
I AM VERY CONFUSED.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16811
|
|
Simran Dass wrote:
Cannot understand why its always ABC , shouldn't it be XYZ because as main thread has acquired a lock on object td so even if td.start() is executed the
run() method cannot get into synchronized(this) as the lock is already acquired by main() method.
I AM VERY CONFUSED.
Take a look again. The two threads are using two different locks.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Simran Dass
Ranch Hand
Joined: Jan 09, 2010
Posts: 183
|
|
Is it so that main() thread is locking on object td whereas in case of synchronized(this)
the lock is on the object created using new xmap().
Please reply.
|
 |
Brij Garg
Ranch Hand
Joined: Apr 29, 2008
Posts: 234
|
|
Yes, You have correctly figured it now
|
 |
Rajeev Trikha
Ranch Hand
Joined: Jan 29, 2010
Posts: 85
|
|
rather than combining both the thread and the job (the code in the run()method) into one class, you've split it into two classes—the Thread class for the thread-specific code and your Runnable implementation class for your job-that-should-be-run-by-a thread code. (Another common way to think about this is that the Thread is the "worker," and the Runnable is the "job" to be done.)
is the pertinent explanation in K&B on runnable.
|
 |
piyush maheshwari
Greenhorn
Joined: Aug 24, 2009
Posts: 4
|
|
The two threads are acquiring locks on different objects.
Secondly main thread is off to sleep so chances of printing ABC is almost always.
In such questions almost always is the correct answer as probability of happening other is negligible.
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
Here, We create a Thread object in the main method, called td. Later part of the main method is synchronized on object td and we invoke the start() method on that Thread. Now it creates a separate thread stack. And we put the main Thread into sleep just for 10 milliseconds. So the other thread stack will continue its work and print that statement within 10 milliseconds. After main method wake up from its sleep state to runnable state, the other thread stack finished its stack and prints ABC. And now the main Thread assign XYZ to s. Most likely answer is ABC.
Try this also.....
|
 |
Simran Dass
Ranch Hand
Joined: Jan 09, 2010
Posts: 183
|
|
Thankyou everybody.
|
 |
 |
|
|
subject: Thread Question - Examlab Practice Exam 1
|
|
|