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 One more on threads 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 "One more on threads" Watch "One more on threads" New topic
Author

One more on threads

Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
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 gurtaneed to be 0 or 1
j.v guranteed to be 0 or 1
k.v guranteed to be 0 or 1
j.v greater than or = to k.v at any given time
k.v greater than or = to j.v at any given time
No answer provided please help.
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
(a) and (b)
Here is the explanation from the book
if a thread is executing method b() on an object, then it is guaranteed that no other thread executes methods a() and b() concurrently. therefore, the invocation counters i and j will never show more than one concurrent invocation. two threads can concurrently be executing methods b() and c(). therefore the invocation counter k can easily show more than one concurrent invocation. that's all!


Amir
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: One more on threads