This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads & Synchronization 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 "Threads & Synchronization" Watch "Threads & Synchronization" New topic

Threads & Synchronization

v iyer

Joined: Nov 15, 2000
Posts: 3
Which is true is a(), b() & c() can be called at any time
1> i.v is guaranteed always to be 0 or 1
2> j.v is guaranteed always to be 0 or 1
3> k.v is guaranteed always to be 0 or 1
4> j.v will always be >= k.v at any given time
5> k.v will always be >= j.v at any given time
I felt a,b,& c are all true as all methods are synchronized
and hence when the thread is in a() only i is inc & dec
so j & k remain unchanged
similarly b() & c().
But the answer given is only a & b.
Can someone please explain how this works?

class Counter{
int v = 0;
synchronized void inc() {v++;}
synchronized void dec() {v--;}
public class test extends Thread{
Counter i;
Counter j;
Counter k;
public synchronized void a(){;

public synchronized void b(){;;;
i.dec(); j.dec(); k.dec();
public synchronized void c(){;
V Iyer
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Threads & Synchronization
It's not a secret anymore!