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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:10:54+00:00 2026-05-27T01:10:54+00:00

First class: class Main1 { private ExecutorService service = Executors.newFixedThreadPool(4); public static void main(String[]

  • 0

First class:

class Main1 {
    private ExecutorService service = Executors.newFixedThreadPool(4);

    public static void main(String[] args) {
       Main1 m = new Main1();
       m.start();
    }

    public void start() {
        final MyObject obj = new MyObject(); 
        obj.doSomeCalculation();//  after this point not to modify obj in main thread

        service.submit(new Runnable(){
            public void run() {
                    obj.doSomething(); // is it threadsafe doing this?
               }
        });
    }
}

Second class:

class Main2 {
    private ExecutorService service = Executors.newFixedThreadPool(4);

    public static void main(String[] args) {
        Main2 m = new Main2();
        m.start();
    }

    public void start() {
        class Job implements Runnable {
           public MyObject obj;

            public void run() {
                obj.doSomething(); // is it threadsafe doing this?
            }
        }

        Job job = new Job();
        job.obj.doSomeCalculation(); // after this point not to modify obj in main thread
        service.submit(job);
    }
}

Are Main1 and Main2 threadsafe? Does Main1 and Main2 make different sense to thread-safety?

update:
neither doSomeCalulation() nor doSomething() don’t have any lock or synchronized block. I want to known whether doSomething() could always read the correct states that doSomeCalculation() change to obj

  • 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-27T01:10:55+00:00Added an answer on May 27, 2026 at 1:10 am

    Are Main1, Main2 threadsafe?

    In the Main1 case, the thread-safety of the application depends on whether MyObject is thread-safe and whether any other threads doing things with it. However, the obj.doSomething(); statement is thread-safe assuming nothing else is changing the object

    In fact, the obj.doSomething(); statement doesn’t use the variable in the enclosing class. Instead, the value of that variable is passed to the inner class in a hidden constructor argument. The other thing that makes this thread-safe is that there is an implicit synchronization between the parent and child threads when a new thread is created. (Reference – JLS 17.4.4 Synchronization Order) These two facts combined mean that the Runnable.run() method will get the correct reference, and that the child thread will see the state of the object at the synchronization point (or later).

    In the Main2 case, the same applies. In this case you are merely doing explicitly (more or less) what is happening implicitly in the Main1 case.

    UPDATE – the above reasoning applies even if you mutate the object in the parent thread (as per your updated question) before passing it to the child thread … because of the implicit synchronization that I mentioned. (However, if the parent thread were to change MyObject after the submit() call, you’d run into thread-safety problems.)

    Does Main1 and Main2 make different sense?

    I don’t know what you are asking. If you are asking if there is any benefit in using an inner class rather than an anonymous inner class … in this case the answer is no. They behave the same with respect to thread-safety.

    Actually, the Main1 version is better because its is simpler, more readable (to an experienced Java developer), and more robust. The Main2 class exposes the obj field for other code to potentially access or even update. That’s bad style. You could fix that, but only by adding more code … which brings us back to simplicity / readability.

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

Sidebar

Related Questions

public class Main { public static void main (String args[]) { int nums[]= {2,
Here is my code: public class Main { public static void main(String[] args) {
Main.java import java.io.IOException; public class Main { private final CompressedOutputStream m_cos; public static void
public Class A { public A() { someotherclass.someevent += new EventHandler(HandleEvent); } private void
Given the following code : import java.io.*; public class Main { public static void
I am using this simple code public class Main extends Activity { private ProgressDialog
public class Normal { public string name; // name is public public String getName()
package com.parseador.prueba; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class main extends
How to Solve java.lang.StackOverflowError from following Code? My Code is:- public class main extends
I'm creating my first class, mainly guided by Overland's C++ Without Fear. I've made

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.