• 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Mughal rasmussen question

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
where is the main method ?
what is the order of the calling of the functions a,b,c ?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Can't .... do .... plaid .... So I did this tiny ad instead:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic