Forums Register Login

Question on thread topic

+Pie Number of slices to send: Send
Hi,
This is a question from an online mock exam found in this site:
web page

What is the output of the following code when compiled and run? Select two correct answers.



A. The output will always be a.i=0 a.i=0
B. The output cannot be determined.
C. The output will always be a.i=0 a.i=-1
D. t1 and t2 access the same member variable a.i.
E. Compilation error.

Answer given is : B,D

I tried to compile and run this code but the nothing was printed.

My answer was A and D.
Please help.

Thank you
+Pie Number of slices to send: Send
I'm not sure why you didn't get any output. If your machine is slow, maybe a million times through the loop is too many. Try changing it to 1000.

Each thread increments i and then decrements it, many times. So by the end i is back to zero. But whichever thread finishes first might print out an intermediate state. Suppose the quicker thread finishes at a moment when the slower thread has just incremented i. Now i is 1, and that's what will be printed out. Or if the quicker thread finishes at a moment when the slower thread has just decremented i, and it will print out zero.

So (B) is correct: the result is indeterminate. And you're right about (D).

-- Phil
+Pie Number of slices to send: Send
Hello,
I am confused with the output I get on this question 52. Tried it using Eclipse and the output I get all the time is a.i=0 a.i=0 (A). How is that?

Thanks.

Roopesh.
+Pie Number of slices to send: Send
Roopesh,
As Philip said, the output can't be determined at all. In your case its just a coincidence that the output is the same every time.

Try this : change the counter from a million to 10, and run this program in two separate command windows quickly a few times ... you should be able to get a different answer atleast once (if you are lucky

Since this is just a mock question, don't worry about the output, understand the concept.
+Pie Number of slices to send: Send
And, Threads execution will vary depending upon JVM/OS. So, In most of the cases the output will not match. So, B is the correct answser.
+Pie Number of slices to send: Send
I want to know if bMethod() is executed concurrently even the synchronized block.
Thank you
+Pie Number of slices to send: Send
 

Originally posted by Adil El mouden:
I want to know if bMethod() is executed concurrently even the synchronized block.
Thank you


The bMethod can be called concurrently, but the synch block can only be called in a synchronized manner,i.e., one at a time.. (ofcourse, as long as its the same object "a")

For more information:
This kind of construct is mostly used in the singleton factory methods and is also known as Double Checked Locking (DCL), try searching for it in this forum or google it.
[ August 12, 2005: Message edited by: Sachin Ahuja ]
+Pie Number of slices to send: Send

The above code(originally posted) has same effect as the code below, since no other code is present in bMethod()

so, bMethod() is not concurrently executed.

Actually, threads are executed concurrently. But 'synchronized' puts a lock in the synchronized method(or synchronized block) and does not allow other threads to access that piece of code until the current thread releases the lock.

Does it answer your question?
+Pie Number of slices to send: Send
 

Originally posted by Arulkumar Gopalan:

The above code(originally posted) has same effect as the code below, since no other code is present in bMethod()

so, bMethod() is not concurrently executed.

Actually, threads are executed concurrently. But 'synchronized' puts a lock in the synchronized method(or synchronized block) and does not allow other threads to access that piece of code until the current thread releases the lock.

Does it answer your question?



How the output cannot be determined if aMethod() and bMethod() are both synchronized?
I missed something?
Please explain

:roll:
+Pie Number of slices to send: Send
Please help :roll:
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 786 times.
Similar Threads
Thread behavior with synchronized method
Doubt with thread
even on synchronizing, why o/p cannot be determined??
[B]Class Monitor Vs. Object Monitor[/B]
Thread
simple doubt with operators
Doubt with constructor overloading
Thread - The # between static synchronized and non static synchronized
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 14:10:24.