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...
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)
Joined: Jan 09, 2010
BUT IF THE OPTIONS ARE JUST
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"
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.
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.)
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.
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.....
Joined: Jan 09, 2010
subject: Thread Question - Examlab Practice Exam 1