• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

API methods and synchronization

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I call Random.nextLong() and HashMap.toString() from code that is accessed by multiple threads. Should calls to these methods be synchronized? I believe Random is threadsafe. I know that HashMap is not, but I'm not sure about toString().

This question may be better suited to the Threads forum, but since the issue came up while working on the SCJD exam I'll post it here.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Mike. I don't know why you need to use the Random class when the record numbers are calculated from the Sun data file. The toString() method in HashMap isn't synchronized.
 
Mike Hays
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy,

I use the Random class' nextLong() method to generate lock cookies.

Since toString() is not synchronized does that mean I need something like



where masterLock is used to protect lockedRecords?
reply
    Bookmark Topic Watch Topic
  • New Topic