Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronization Methods

 
Swapna Anand
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain this question from K&M mock

options:
A i.v is guranteed always to be 0 or 1
B j.v is guranteed always to be 0 or 1
C k.v is guranteed always to be 0 or 1
D j.v will always be greater than or equal to k.v at any given time
E k.v will always be greater than or equal to j.v at any given time

[B](formatted and placed between tags)[/B]
[ August 18, 2005: Message edited by: Barry Gaunt ]
 
Arul Prasad
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i cant understand the question is there any explanation avail ? in my assumption the above code surely throw a error since i,j,k are only declared not initialised
 
Choopong Songcharoenkij
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I've ever seen this question somewhere before.
I think the question asks us what are true about using KM02 in multi-thread environment. Additonally, we must suppose that all Counter instance variables in KMO2 are instantiated properly before any methods called.

First, see the Counter class.
1) because inc() and dec() have a synchronized keyword, they are atomic and mutually exculsive.
2) int v, is a package-private variable. Although all methods of this class are synchronized, other classes in the same package as Counter can access the variable directly. If it happens, the value of v is undetermined.

Second, consider the KM02 class.
1) the same as above, i, j, and k are package-private member variables.
2) a() and b() are mutually exclusive. Counter value used in these methods are unchanged because of calling both inc() and dec() once.
3) c() is not synchronized, calling this method can intervene a() and b().

Finally, see the options.
A, B. is true because i and j are only modified in the synchronized method.
C. is false due to c(), which modify k, is not a synchronized method.

because of k.v is undetermined, D. and E. are false.

Ok, This is all my opinion.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic