Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8908799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:16:30+00:00 2026-06-15T03:16:30+00:00

I have rather general question, please advice. I have a servlet. This servlet has

  • 0

I have rather general question, please advice.

I have a servlet.

This servlet has private field.

Private field is a kind of metadata stuff (public class Metadata{//bla-bla-bla}).

When GET request is processed, this metadata is used to perform some operation.

I want to implement POST method in the same servlet. User uploads file and Metadata field is updated.

The problem: concurrent access to this private field with Metadata object shared among sereval web-threads using one servlet instance. POST method operaton (Update Metadata object) can lead to Metadata inconsistent state and concurrent GET request can be failed.

The question: what is the best way to update Metadata object while GET requests are running?

Dummy solution:

  1. During each GET request,, at the very beginning

  2. Synchonize Metadata object and clone it in one block, then release it.

  3. Concurrent GET requests work with clone verstion of Metadata object which is consistent.

  4. During each POST request.

  5. Synchonize Metadata object and update its fields.

  6. Release Metadata object.

Please advice or critisize.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:16:31+00:00Added an answer on June 15, 2026 at 3:16 am

    Using synchronized methods set and get in the Metadata class is fine but may slower your web app in case you have multiple readers and (much) less writers:

    Java synchronized keyword is used to acquire a exclusive lock on an
    object. When a thread acquires a lock of an object either for reading
    or writing, other threads must wait until the lock on that object is
    released. Think of a scenerio that there are many reader threads that reads a shared
    data frequently and only one writer thread that updates shared data.
    It’s not necessary to exclusively lock access to shared data while
    reading because multiple read operations can be done in parallel
    unless there is a write operation.

    (Excerpt from that nice post)

    So using a multiple read single write strategy may be better in term of performance in some cases as explained also in the same Java5 ReadWriteLock interface doc:

    A read-write lock allows for a greater level of concurrency in
    accessing shared data than that permitted by a mutual exclusion lock.
    It exploits the fact that while only a single thread at a time (a
    writer thread) can modify the shared data, in many cases any number of
    threads can concurrently read the data (hence reader threads). In
    theory, the increase in concurrency permitted by the use of a
    read-write lock will lead to performance improvements over the use of
    a mutual exclusion lock. In practice this increase in concurrency will
    only be fully realized on a multi-processor, and then only if the
    access patterns for the shared data are suitable.

    Whether or not a read-write lock will improve performance over the use
    of a mutual exclusion lock depends on the frequency that the data is
    read compared to being modified, the duration of the read and write
    operations, and the contention for the data – that is, the number of
    threads that will try to read or write the data at the same time. For
    example, a collection that is initially populated with data and
    thereafter infrequently modified, while being frequently searched
    (such as a directory of some kind) is an ideal candidate for the use
    of a read-write lock. However, if updates become frequent then the
    data spends most of its time being exclusively locked and there is
    little, if any increase in concurrency. Further, if the read
    operations are too short the overhead of the read-write lock
    implementation (which is inherently more complex than a mutual
    exclusion lock) can dominate the execution cost, particularly as many
    read-write lock implementations still serialize all threads through a
    small section of code. Ultimately, only profiling and measurement will
    establish whether the use of a read-write lock is suitable for your
    application.

    A ready to use implementation is the ReentrantReadWriteLock.

    Take a look at the previous post for a nice tutorial on how to use it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a general question that is rather open-ended (i.e. depends on platform, application
I have a rather general question regarding JavaScript and local variables. My question is
I have a rather basic and general question about atomicity of UPDATE ... SET
this time I got a more general question. Should I use multiple views rather
This is a rather general question on what actually happens when an app returns
This is a rather general question .. What hardware setup is best for large
I have a rather general question regarding the recognition speed of any dictionary using
This is more of a general question rather than a specific problem. I'm coding
This is more of a general best practice question rather than a very focused
Have a rather simple question. Does anyone knows if i can use jparallax both

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.