I have some some values in database which will be updated automaticaly. I want to show to the user current values which are in database.
For this I have made one thread which will be instantiated on startup by a servlet using <load-on-startup> tag.
The Thread and other servlets will be sharing an Object which will be having an application scope.
Thread is having a function which will change the state of the Object. I have synchronized Threads writing function and servlets reading function on this Objects Lock.
Now, when servlet is reading the Object's properties Threads write function will not execute. This Ok. But my other Servlets cant read concurrently because the Objects lock is aquired by other reading Servlet. I want all Servlets reading concurrently.
There are other types of locks that allow concurrent reads but only exclusive writes, like ConcurrentReadWriteLock. But there is no shortcut to learning about concurrency properly, otherwise your code is unlikely to be thread-safe. I suggest to work through one of the eminent books: https://coderanch.com/how-to/java/ThreadsAndSynchronizationFaq
Whose rules are you playing by? This tiny ad doesn't respect those rules: