Given the following code, which statements are true about the objects referenced through the fields i, j, and k, 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();
}
}
Select the two correct answers.
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 greater than or equal to k.v at any give time.
5)k.v will always be greater than or equal to j.v at any give time.
1 and 2 are correct, I agree with that.
I think 5 answer is also right.
What you ppl think.