I know that singleton classes are inherently not thread-safe. But is there any way to make it so? I have a singleton in a java web application, and I want one class to exist per user of this program.
This is an issue because the singleton holds state, otherwise it’d be fine.
It makes no sense to make it a singleton. Just make it a session attribute. The
HttpSessionis tied to the browser session and specific for every visitor. For example, the shopping cart:It can just be a simple Javabean.
See also: