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

  • SEARCH
  • Home
  • 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 8199573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:11:53+00:00 2026-06-07T06:11:53+00:00

I have a object obj that is read by many threads frequently , but

  • 0

I have a object obj that is read by many threads frequently , but updated by only one thread periodically.The update happens after long some interval (say 10 minutes).

The data is less transnational.Meaning if read threads gets stale data(old) for some time then its perfectly ok.

Now i thought of using following approach for synchronization:

final Object lock = new Object();
private MyObject obj = new MyObject(); //this is the data

public String getDataFieldName(){
 synchronized(lock){
   return this.obj.name;
 }
}


/*the following code is wrong right?As its just synchronizes the code for getting reference.But after getting reference read thread R1 may try to get data while write Thread is modifying data.Will that give exception?How to solve this? */
public String getData(){
 synchronized(lock){
   return this.obj;
 }
}

//only one thread can update.But how multipe threads can read at once?
public updateData(args ) {
  synchronized(lock){
    //do update
 }

}

My questions are as follows:

  1. I dont want only one thread to read the data.Reads should be parallel.

  2. How can i synchronize read and write ?If write thread is updating and read thread is reading i dont what to get some exception.It is ok if read gets some old data
    3)If read thread is reading while write thread is updating , will i get exception?Will there be any problem?

  • 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-07T06:11:54+00:00Added an answer on June 7, 2026 at 6:11 am

    You don’t need any synchronization in this scenario. All you have to do is the following:

    • Make sure MyObject is immutable, meaning that you never change any values in your object Instead you construct a new MyData object everytime you change it. This prevents anyone from seeing a half-changed object.
    • Declare obj as volatile, to make sure all threads see the updated value everytime.

    You will never get an exception because of concurrent reads and writes if you follow these steps.

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

Sidebar

Related Questions

I have an object obj that is passed into a helper method. public static
I have read that when you override Equals on an class/object you need to
I don't know how interface works for my problem but I have read that
I have the following method public static void SerializeToXMLFile(Object obj,Type type, string fileName) {
I have this interface: public interface IValidationCRUD { public ICRUDValidation IsValid(object obj); private void
I have a Java function called testForNull public static void testForNull(Object obj) { if
i have a method as follows. test(Object obj){ } now within this method i
I have below code which overrides equals() and hashcode() methods. public boolean equals(Object obj)
I have an on object like so var obj = {}; function set() {
I have the following object: var r = { obj: $(this), children: $(this).children(), panes:

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.