wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mughal rasmussen question 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 "Mughal rasmussen question" Watch "Mughal rasmussen question" New topic
Author

Mughal rasmussen question

Mahen
Greenhorn

Joined: Feb 20, 2000
Posts: 23
Given the following code, which statements concerning the objects referenced through the member variables i, j and k are true, given that any thread may call the methods a, b and c at any time?
class Counter {
int v = 0;
synchronized void inc() { v++; }
synchronized void dec() { v--; }
}
public class Q7ed5 {
Counter i;
Counter j;
Counter k;
public synchronized void a() {
i.inc();
System.out.println("a");
i.dec();
}
public synchronized void b() {
i.inc(); j.inc(); k.inc();
System.out.println("b");
i.dec(); j.dec(); k.dec();
}
public void c() {
k.inc();
System.out.println("c");
k.dec();
}
}

i.v isguaranteed alway to be 0 or 1
j.v isguaranteed alway to be 0 or 1
k.v isguaranteed alway to be 0 or 1
j.v will always be greater than or equal to k.v at any given time
k.v will always be greater than or equal to j.v at any given time
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
where is the main method ?
what is the order of the calling of the functions a,b,c ?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
javauser - the idea here is that the public methods in this code could be called in any order imaginable by some unknown user of the package. There could even be multiple threads which are trying to call the same method simultaneously (or nearly so) - what would happen? It is possible to make some general statments about what results are possible - see if you can decides which statements are true.
Note - technically, none of the statements are true, because M & R neglected to make i, j, and k private, so it is possible for another programmer to call inc() and dec() directly on these instances and get any values she wants to for v - but I'm pretty sure this is not the intent of the question, so just pretend that i, j, and k were all declared private.

"I'm not back." - Bill Harding, Twister
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Note that method c is not synchronized at all. Therefore any number of threads may call method c at any time so no conclusions can be made about the value of k.v at any given time.
i.v is modified by both methods a and b. These methods are synchronized but no synchronization exists BETWEEN these methods. Nothing prevents these methods from running simultaneously in different threads. The synchronization only serializes their individual invocation.
Counter j is modified only in method b. This method, being synchronized can only be called by one thread at a time. So j.v is always guaranteed to be 0 or 1 is the only true statement offered.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
"Note that method c is not synchronized at all. Therefore any number of threads may call method c at any time so no conclusions can be made about the value of k.v at any given time."
Well, none of the conclusions listed are correct. It's possible to make other conclusions, like "k.v is always >= 0". And the last option was almost true - if method b() had listed k.inc() before j.inc(), then it would have been possible to say that k.v was always >= j.v.
"i.v is modified by both methods a and b. These methods are synchronized but no synchronization exists BETWEEN these methods. Nothing prevents these methods from running simultaneously in different threads. The synchronization only serializes their individual invocation."
Incorrect. Both a() and b() are competing for a lock on the same object, which is the instance of Q7ed5 that is being used to invoke the methods. Neither method can run if the other has the lock.
"Counter j is modified only in method b. This method, being synchronized can only be called by one thread at a time. So j.v is always guaranteed to be 0 or 1 is the only true statement offered."
Both i.v and j.v are guaranteed to be 0 or 1.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hmmm, thanks Jim for clearing up my misunderstanding. I was thinking the lock exists on the method, but you're right, the lock exists on the OBJECT that called the method. Thanks again.
[This message has been edited by Joe Java (edited February 22, 2000).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mughal rasmussen question