File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

API methods and synchronization

 
Mike Hays
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
K. Tsang
Bartender
Pie
Posts: 3222
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic