• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Synchronization Methods

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
A feeble attempt to tell you about our stuff that makes us money
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic