Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Thread Question - Examlab Practice Exam 1

Simran Dass
Ranch Hand
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
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 ... ...

Abimaran Kugathasan
Ranch Hand
Posts: 2066

Try this also........

Harpreet Singh janda
Ranch Hand
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
Sheriff
Posts: 9543
33
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...

Abimaran Kugathasan
Ranch Hand
Posts: 2066
Thanks Ankit....

Rajeev Trikha
Ranch Hand
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.

Simran Dass
Ranch Hand
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
Marshal
Posts: 21745
85
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

Simran Dass
Ranch Hand
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().

Brij Garg
Ranch Hand
Posts: 234
Yes, You have correctly figured it now

Rajeev Trikha
Ranch Hand
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
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
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
Posts: 183

Thankyou everybody.