Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple Servlets Communicating with a thread

 
Mukesh Negi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a thread which will change(write) the state of an object.

There will be many Servlets that will be reading the same object.

I want this reading and writing synchronized.


Please guide me how I can achieve this.

 
vinayak jog
Ranch Hand
Posts: 83
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any good java book should guide you. Please post specific queries dont just paste your assignments
 
vinayak jog
Ranch Hand
Posts: 83
MySQL Database Netbeans IDE Ubuntu
 
Mukesh Negi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vinayak this is not an assignment..

I am sory that I was not specific with my query..

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.

Please guide me.

 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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://www.coderanch.com/how-to/java/ThreadsAndSynchronizationFaq
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic