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???
catchbollu entered in
sunil choudhary
Ranch Hand
Joined: Nov 10, 2000
Posts: 138
posted
0
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
----------------------------------<br />"Learning is weightless, a treasure you can always carry easily."<br />-Chinese Proverb
bill bozeman
Ranch Hand
Joined: Jun 30, 2000
Posts: 1070
posted
0
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
Panagiotis Varlagas
Ranch Hand
Joined: Nov 27, 2000
Posts: 233
posted
0
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.