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 3939830
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:20:45+00:00 2026-05-20T00:20:45+00:00

I have a java threads related question. To take a very simple example, lets

  • 0

I have a java threads related question.

To take a very simple example, lets say I have 2 threads.

Thread A running StockReader Class instance

Thread B running StockAvgDataCollector Class instance

In Thread B, StockAvgDataCollector collects some market Data continuously, does some heavy averaging/manipulation and updates a member variable spAvgData

In Thread A StockReader has access to StockAvgDataCollector instance and its member spAvgData using getspAvgData() method.

So Thread A does READ operation only and Thread B does READ/WRITE operations.

Questions

  1. Now, do I need synchronization or atomic functionality or locking or any concurrency related stuff in this scenario? It doesnt matter if Thread A reads an older value.

  2. Since Thread A is only going READ and not update anything and only Thread B does any WRITE operations, will there be any deadlock scenarios?

I’ve pasted a paragraph below from the following link. From that paragraph, it seems like I do need to worry about some sort of locking/synchronizing.

http://java.sun.com/developer/technicalArticles/J2SE/concurrency/

Reader/Writer Locks

When using a thread to read data from an object, you do not necessarily need to prevent another thread from reading data at the same time. So long as the threads are only reading and not changing data, there is no reason why they cannot read in parallel. The J2SE 5.0 java.util.concurrent.locks package provides classes that implement this type of locking. The ReadWriteLock interface maintains a pair of associated locks, one for read-only and one for writing. The readLock() may be held simultaneously by multiple reader threads, so long as there are no writers. The writeLock() is exclusive. While in theory, it is clear that the use of reader/writer locks to increase concurrency leads to performance improvements over using a mutual exclusion lock. However, this performance improvement will only be fully realized on a multi-processor and the frequency that the data is read compared to being modified as well as the duration of the read and write operations.

Which concurrent utility would be less expensive and suitable in my example?

java.util.concurrent.atomic ?

java.util.concurrent.locks ?

java.util.concurrent.ConcurrentLinkedQueue ? – In this case StockAvgDataCollector will add and StockReader will remove. No getspAvgData() method will be exposed.

Thanks
Amit

  • 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-05-20T00:20:45+00:00Added an answer on May 20, 2026 at 12:20 am

    Well, the whole ReadWriteLock thing really makes sense when you have many readers and at least one writer… So you guarantee liveliness (you won’t be blocking any reader threads if no one other thread is writing). However, you have only two threads.

    If you don’t mind thread B reading an old (but not corrupted) value of spAvgData, then I would go for an AtomicDouble (or AtomicReference, depending on what spAvgData’s datatype).

    So the code would look like this

    public class A extends Thread {
      // spAvgData
      private final AtomicDouble spAvgData = new AtomicDouble(someDefaultValue);
    
      public void run() {
        while (compute) {
         // do intensive work
         // ...
          // done with work, update spAvgData
         spAvgData.set(resultOfComputation);
        }
      }
    
      public double getSpAvgData() {
        return spAvgData.get();
      }
    }
    // --------------
    
    public class B {
      public void someMethod() {
        A a = new A();
        // after A being created, spAvgData contains a valid value (at least the default)
        a.start();
        while(read) {
          // loll around
          a.getSpAvgData();
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Java Threads and synchronization. Lets say I have: public class MyClass(){
I have this very straight forward question regarding Thread and Timer classes in Java
I have a layered application in Java which has a multi thread data access
I have a java back-end that needs to expose services to clients running in
I have a question that is related to possible overhead of ExecutorService s in
This question is related to my question on existing coroutine implementations in Java .
Related: Does java have a "LinkedConcurrentHashMap" data structure? I am looking for a collection
I have Java and Flash client applications. What is the best way for the
Here is what I have: JAVA_HOME=C:\Software\Java\jdk1.5.0_12 (points to JDK 5.0) In Eclipse Installed Runtimes
Does Java have a built-in way to escape arbitrary text so that it can

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.