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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:41:38+00:00 2026-06-16T13:41:38+00:00

This seems to be a pretty frequent question though I didn’t find one. Suppose

  • 0

This seems to be a pretty frequent question though I didn’t find one. Suppose I have this piece of code:

public class MyClass {
    private AnotherClass mField;

    public void changeOne(AnotherClass newOne) {
        // <...> lines of code here
        synchronized (mField) {
            mField = newOne;
        }
        // <...> lines of code here

    }

    public void changeTwo(AnotherClass newTwo) {
        // <...> lines of code here
        mField = newTwo;
        // <...> lines of code here
    }
}

Let’s say changeOne() and changeTwo() are called from different threads. Is it enough to have a synchronized block in changeOne() to protect mField from changing by changeTwo()? Or I need to explicitly wrap each place where mField is changed into the synchronized block? (please leave behind synchronized methods and others).

  • 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-16T13:41:39+00:00Added an answer on June 16, 2026 at 1:41 pm

    You need to explicitly synchronize all modifications to mField using either synchronized block (or) synchronized method. Otherwise more than one thread can change mField by executing changeTwo at a time.

    EDIT: As Tedd Hopp suggested, if variable is not volatile reads also need to synchronized and lock you get should be on same object.

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

Sidebar

Related Questions

I've been pretty puzzled by this one. It seems as though my implementation of
This seems pretty a straight-forward question, but I could not find a specific answer
This seems pretty basic, but I can't find the best method to do this...
So this seems pretty simple and it probably is. I have a textView and
So this seems pretty basic but I can't get it to work. I have
I know this seems pretty basic, and it should be, but I can't find
I am trying to get my head around this query - seems pretty straight
This seems like it should be pretty straight forward, but I'm apparently confused. I
Alright I don't see why this isnt working. It seems pretty simple. Here is
Getting friends of friend are pretty easy, I got this which seems to work

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.