This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Multiple Servlets Communicating with a thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Multiple Servlets Communicating with a thread" Watch "Multiple Servlets Communicating with a thread" New topic
Author

Multiple Servlets Communicating with a thread

Mukesh Negi
Greenhorn

Joined: Jul 25, 2012
Posts: 17
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

Joined: Apr 01, 2011
Posts: 81

Any good java book should guide you. Please post specific queries dont just paste your assignments
vinayak jog
Ranch Hand

Joined: Apr 01, 2011
Posts: 81

link that may help you http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html
Mukesh Negi
Greenhorn

Joined: Jul 25, 2012
Posts: 17
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
Marshal

Joined: Mar 22, 2005
Posts: 41083
    
  43
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


Ping & DNS - my free Android networking tools app
 
wood burning stoves
 
subject: Multiple Servlets Communicating with a thread
 
Similar Threads
Very argent file path
Servlets and Threading
Servlet Materials
Update web page with data from other pages?
servlet session thread safety ?