• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Multiple Servlets Communicating with a thread

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 83
Netbeans IDE MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
link that may help you http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html
 
Mukesh Negi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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://coderanch.com/how-to/java/ThreadsAndSynchronizationFaq
 
Whose rules are you playing by? This tiny ad doesn't respect those rules:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic