Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic