The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Synchronization Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Synchronization Methods" Watch "Synchronization Methods" New topic

Synchronization Methods

Swapna Anand

Joined: May 11, 2005
Posts: 7
Please explain this question from K&M mock

A i.v is guranteed always to be 0 or 1
B j.v is guranteed always to be 0 or 1
C k.v is guranteed always to be 0 or 1
D j.v will always be greater than or equal to k.v at any given time
E k.v will always be greater than or equal to j.v at any given time

[B](formatted and placed between tags)[/B]
[ August 18, 2005: Message edited by: Barry Gaunt ]
Arul Prasad
Ranch Hand

Joined: Jan 20, 2005
Posts: 57
i cant understand the question is there any explanation avail ? in my assumption the above code surely throw a error since i,j,k are only declared not initialised

With Regards<br />Arul
Choopong Songcharoenkij

Joined: Sep 28, 2004
Posts: 18
Hi, I've ever seen this question somewhere before.
I think the question asks us what are true about using KM02 in multi-thread environment. Additonally, we must suppose that all Counter instance variables in KMO2 are instantiated properly before any methods called.

First, see the Counter class.
1) because inc() and dec() have a synchronized keyword, they are atomic and mutually exculsive.
2) int v, is a package-private variable. Although all methods of this class are synchronized, other classes in the same package as Counter can access the variable directly. If it happens, the value of v is undetermined.

Second, consider the KM02 class.
1) the same as above, i, j, and k are package-private member variables.
2) a() and b() are mutually exclusive. Counter value used in these methods are unchanged because of calling both inc() and dec() once.
3) c() is not synchronized, calling this method can intervene a() and b().

Finally, see the options.
A, B. is true because i and j are only modified in the synchronized method.
C. is false due to c(), which modify k, is not a synchronized method.

because of k.v is undetermined, D. and E. are false.

Ok, This is all my opinion.

SCEA5.0 (In Progress), SCBCD5.0, SCBCD1.3, SCWCD5.0, SCWCD1.4, SCJD, SCJP6.0, SCJP5.0, SCJP1.4, SCJA1.0<br />OMG Certified UML 2.0 Professional - Fundamental - Intermediate - Advanced <br />Oracle Database 10g Administrator Certified Associate
I agree. Here's the link:
subject: Synchronization Methods
It's not a secret anymore!