File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on thread topic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on thread topic" Watch "Question on thread topic" New topic
Author

Question on thread topic

Adil El mouden
Ranch Hand

Joined: Jun 01, 2005
Posts: 82
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


SCWCD 1.4(Loading...), SCJP 1.4(98%), Bachelor of Engineering (computer science)
Philip Heller
author
Ranch Hand

Joined: Oct 24, 2000
Posts: 119
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


Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
Roopesh Gulecha
Ranch Hand

Joined: Aug 09, 2005
Posts: 32
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

Joined: Aug 12, 2005
Posts: 13
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

Joined: Oct 13, 2003
Posts: 104
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.


Anbudan & Mahalo,<br />Arul<br /> <br />-Not a sun certified Java professional :-)
Adil El mouden
Ranch Hand

Joined: Jun 01, 2005
Posts: 82
I want to know if bMethod() is executed concurrently even the synchronized block.
Thank you
Sachin Ahuja
Greenhorn

Joined: Aug 12, 2005
Posts: 13
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

Joined: Oct 13, 2003
Posts: 104

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

Joined: Jun 01, 2005
Posts: 82
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

Joined: Jun 01, 2005
Posts: 82
Please help :roll:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on thread topic
 
Similar Threads
Doubt with thread
Thread
even on synchronizing, why o/p cannot be determined??
[B]Class Monitor Vs. Object Monitor[/B]
Thread behavior with synchronized method