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

random( )

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Math class has a function random that is : -
static synchronized double random()
i want to know why is it declared static and synchronized,
what is static and synchronized doing in function random???
 
Ranch Hand
Posts: 144
Redhat Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi catch you hve a strange name ??
anyway
Almost al of the methods in MAth calss are static as we need to use them on the fly. Declaring them static makes them universally available.
Why are they syncronized ,I am not sure about that.
sunil Choudhary
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All methods in Math are static because you can't construct an instance of Math, the constructors are private, so you can't use them.
As for synchronization, when I looked up the API I got this:
public static double random()
so, random() isn't synchronized.
Also, catchbollu, does not conform to our naming policy here at the Ranch. We appreicate your posts and questions and we want you to continue, but can you please re-register with a more appropriate name.
Thanks,
Bill
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bill,
It *is* synchronized.
<pre>
=============
public static synchronized double random() {
if (randomNumberGenerator == null)
randomNumberGenerator = new Random();
return randomNumberGenerator.nextDouble();
}
=============
</pre>
A method being synchronized or not is considered to be an implementation detail, and thus is not part of the contract for that method. So, it is prudent that such info is not included in the API.
Now, as per the reason it is marked as synchronized. Well, it sets private static variable randomNumberGenerator of Math. You want to have synchronized access to such code to avoid having concurrently running threads one overwrite the acts of the other one.
Panagiotis.
 
no wonder he is so sad, he hasn't seen this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic