Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on thread topic

 
Adil El mouden
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Philip Heller
author
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Roopesh Gulecha
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sachin Ahuja
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arulkumar Gopalan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Adil El mouden
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know if bMethod() is executed concurrently even the synchronized block.
Thank you
 
Sachin Ahuja
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Arulkumar Gopalan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Adil El mouden
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Adil El mouden
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help :roll:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic