• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Question on thread topic

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help :roll:
 
And then the entire population worshiped me like unto a god. Well, me and this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic