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?
i.v is guranteed always to be 0 or 1 j.v is guranteed always to be 0 or 1 k.v is guranteed always to be 0 or 1
j.v will always be greater than or equal to k.v at any give time k.v will always be greater than or equal to k.v at any give time
I donot know right the answer and cannot make head or tails out of this question. Can some one help me understand this and how to approach such questions. [ September 08, 2008: Message edited by: Nabila Mohammad ]
The future belongs to those who believe in the beauty of their dreams.Dream BIG!
I think the answer is i.v and j.v are guaranteed to be either 0 or 1.
Well you will have to understand the question as this
there are suppose three threads. first thread(thread1) invokes b(). it increases the value of i.v so it becomes 1. now another thread(thread2) calls a(), the thread will have to wait as both a() and b() are synchronized. then thread1 increases j.v, then k.v so both become 1, now third thread(thread3) invokes c(). the call will be made immediately since c() is not synchronized. it will increate k.v so it will be 2.
So as you can see that i.v will be increased by one and then decreased by 1 before any other thread can increase or decrease it(as a() and b() are synchronized).
Similarly j.v will be increased by one and then decreased by 1 before any other thread can increase or decrease it(b() is synchronized).
but k.v can be increased by one more than one time concurrently as c() is not synchronized...