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.
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).
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?
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.
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 ]
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.
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