Say I have two classes like this:
class A{
private static Random random = new Random();
public A(){
// Do something.
}
public Integer methodGetsCalledQuiteOften(){
return random.nextInt();
}
}
class B{
private Random random;
public A(){
random = new Random();
// Do something.
}
public Integer methodGetsCalledQuiteOften(){
return random.nextInt();
}
}
In a scenario where both of them get instantiated multiple times and both of these classes’ instances’ method methodGetsCalledQuiteOften gets called a lot, is there any real advantage/disadvantage (time, memory) in using a static variable that holds Random() in class A as opposed to creating a new Random() object in every single instance, like in class B?
The application is multithreaded and higher level of randomness is so I think I am going with static SecureRandom. If that will be a real speed factor after profiling I might choose something else.
Real advantage/disadvantages depend on real code. In other words, it depends on how often a
Bis created compared to how often the method is called, etc. You should profile your application and see whether it makes a reasonable difference.Will A be more performant than B? Certainly. But whether you’ll ever notice depends on your usage. Will A use less memory than B? Certainly, but whether you care or not depends on how many instances of A/B you’re keeping around.
Really the only other consideration is determinism. Since you don’t specify the seed for the Random instance, I take it you don’t care whether you can reproduce the sequence of numbers from the Random. But it’s worth noting…if you have a shared Random, it will be much harder to guarantee a certain deterministic sequence of numbers for some instance of A than it is with one per instance as in B.