This week's book giveaway is in the OCAJP 8 forum.We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!

Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485

1. i.v guaranteed always to be 0 or 1
2. j.v guaranteed always to be 0 or 1
3. k.v guaranteed always to be 0 or 1
4. j.v will always be greater than or equal to k.v at any given time.
5. k.v will always be greater than or equal to j.v at any given time.
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
I guess the answer should be both 1 and 2.
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 20165

46

Originally posted by Parameswaran Thangavel:
hi

Let's look at the possible answers...

1. i.v guaranteed always to be 0 or 1
2. j.v guaranteed always to be 0 or 1
3. k.v guaranteed always to be 0 or 1
4. j.v will always be greater than or equal to k.v at any given time.
5. k.v will always be greater than or equal to j.v at any given time.

As for the methods, a() and b() are both synchronized. They can't be ran in parallel via different threads. The c() method is not synchronized, it can be ran in parallel, even while a() or b() is running.

(3) is not true as you can have two c() method running in parallel, and having increment the count before decrementing the count -- producing a value of 2. (albeit very short lived)

(4) is not true as a ramification of (3). K can have large values... In theory, hard to produce in practice.

(5) is not true based on how method b() works. It increments j before incrementing k... hence, j is larger than k for the really small period between the two methods.

Now that leaves (1) and (2) which is only modified from a() and b(), and which can only run via a single thread, and always runs in pairs, inc() followed by a dec().

Henry

I agree. Here's the link: http://aspose.com/file-tools