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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:23:49+00:00 2026-06-07T08:23:49+00:00

Consider two threads: threadA and threadB . (E.g. let threadA be the UI thread,

  • 0

Consider two threads: threadA and threadB. (E.g. let threadA be the UI thread, and let threadB an own subclass of Thread.) Regarding data visibility, is it safe to instantiate a class in threadA, pass it to the constructor of threadB, and then use it exclusively from threadB? Let me clarify this with code. (Note that the code is simplified, so this architecture makes sense in the real application in its actual form.)

public class SomeClass {
    public SomeClass(....) {
       // initialization
    }
}

public class MyCustomThread extends Thread {
    public MyCustomThread(SomeClass someClass) {
        mSomeClass = someClass;
    }

    @Override
    public void run() {
           // operate on mSomeClass         
    }
}

And the method that starts everything is as follows (its location is irrelevant). This is called by threadA.

public void launchThread() {
   SomeClass someClass = new SomeClass();
   MyCustomThread threadB = new MyCustomThread(someClass); // MyCustomThread is a subclass of Thread
   threadB.start();
   // After this point, threadA never accesses someClass,
   // and only threadB is operating on it
}

I know that constructors are not allowed to be synchronized, and no synchronized code is needed inside it (because the this reference — unless leaked out — is not available to other threads until the class is constructed in its creator thread). So my question is about data visibility: is the code safe, provided I don’t access someClass (and any of its referenced data) from threadA after threadB.start() is called?

In other words, will threadB.run() see the most up-to-date versions of someClass data (such as fields, internal references, etc.)? My guess: since there will be no external change on someClass after threadB accesses someClass for the first time, threadB must see the correct values, because it can’t have cached copies from before. So, for the above reason, I don’t need to access someClass in a synchronized block from threadB.run(). (Again, note that it’s guaranteed that threadA doesn’t access someClass after threadB is started.)

  • 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-07T08:23:51+00:00Added an answer on June 7, 2026 at 8:23 am

    If you’re saying that, after threadB.start(), the instance referred to by someClass will no longer be modified by any other thread: yes, threadB.run() will see the latest version.

    There’s really no concurrency/race-condition that could occur in this scenario. Everything is happening sequentially, you simply have objects that are Threads which I think might be causing you to be slightly paranoid :).

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

Sidebar

Related Questions

Consider the following: ThreadA and ThreadB are two threads writing diagnostic information to a
I have some confusion regarding thread Synchronization . Consider i have two thread Thread1
Consider the Socket.BeginSend() method. If two thread pool threads were to call this method
let's consider two views that use the same layout composed of: A left column
If I have two threads (Linux, NPTL), and I have one thread that is
Consider the following Spring Service class. The spring scope defined is Singleton. The two
Consider two entity classes CD and Track : public class CD { public long
Let's consider the two following lines in C# (using framework .NET 3.5) Regex regex
Consider two extension methods: public static T MyExtension<T>(this T o) where T:class public static
Consider two tables transaction and category each having their own ID and information. A

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.