I would like to create a worker thread that should be shared within other sessions. Basically I want to restrict other users from doing the same process. So They will retrieve the thread via a static Instance of object thread that I created. If the thread is still alive, then they will be prompted with error.
Is there a other way to do this because I am thinking if placing a Thread object within a static is safe? I am also thinking of application context but I am not sure which is better way to do this in java?
Placing any object in a static or in any kind of shared location is not intrinsically unsafe but you need to take care with the design.
declare
initialise
use
Now what happens if two threads hit the initialise block at the same time? You can get two Things created. Probably don’t want that, so use synchronisation.
what happens if two threads attempt to use the t at the same time. This depends on the promises made by Thing. If it’s thread-safe no problem, otherwise your code needs to provide synchronisation