In a specific scenario here,
I have a manger called UserManger used to handle CRUD for user
This manager is “Singleton” and correct by design.
But in update method I have logic
public User update (User u)
{
// This line is problematic?
User u1 = new User();
copy(u,u1);
//Some logic
dao.update(u);
}
Will creating any new objects in singleton manager problematic? Especially for a very concurrent system.
Will
public User update (User u, User u1)
{
copy(u,u1);
//Some logic
dao.update(u);
}
Solve my problem?
Just creating an object within a singleton method won’t cause any problems. You’re not sharing any state between threads, after all.
You would have potential problems if you had state within the singleton itself – but all you’ve shown is creating a local variable, not changing an instance variable. Each invocation of a method has its own entirely separate set of local variables. Two threads can both be executing the same method, but they won’t see each other’s local variables.