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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:10:01+00:00 2026-05-31T14:10:01+00:00

My java code looks like this: class xHandler { private Channel _channel; // Methods…

  • 0

My java code looks like this:

class xHandler
{
    private Channel _channel;

    // Methods...

    void init()
    {
        _channel = new Channel(...);

        synchronized (_channel)
        {
            // Do some stuff here...e.g.
            _channel.send("...");
        }
    }
}

In other files(threads) I create instances of Channel and send stuff with the use of the object, but only in the init method mentioned above I need it to be sync’ed and no other thread should open a channel and send something during this period.

FindBugs gives me a warning called: Method synchronizes on an updated field

This method synchronizes on an object referenced from a mutable field.
This is unlikely to have useful semantics, since different threads may be synchronizing on different objects.

How can I get this issue right? Is it possible to trigger this issue easily with a simple test?

  • 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-31T14:10:02+00:00Added an answer on May 31, 2026 at 2:10 pm
    _channel = new Channel(...);
    synchronized (_channel)
    

    I need it to be sync’ed and no other thread should open a channel and
    send something during this period.

    Seems like your code is not doing what you are thinking that it is doing. Since you are creating a new Channel object per xHandler instance, and acquiring this specific Channel lock, two threads may run concurrently.

    1. xHandler instance 1 creates Channel instance 1 and acquires it’s lock
    2. xHandler instance 2 creates Channel instance 2 and acquires it’s lock
    3. Both run concurrently

    What you really need to do is either:

    1. Share the specific Channel object that you want to sync for multiple xHandler instances (Example: receive a instance of Channel on xHandler constructor and assign it to _channel, remove the _channel = new Channel(...); line and keep your sync code as it is).
    2. Sync on Channel.class which will mean that two different instances of xHandler will never be able to use any Channel simultaneously.

    Example of sharing a specific Channel for two different xHandlers:

    class xHandler
    {
        private final Channel _channel;
    
        public xHandler(Channel channel)
        {
            this._channel = channel  
        }
    
        // Methods...
    
        void init()
        {           
            synchronized (_channel)
            {
                // Do some stuff here...e.g.
                _channel.send("...");
            }
        }
    
                public static void main(String[] args) 
                {
                    Channel myChannel = new Channel(...);
                    xHandler xHand1 = new xHandler(myChannel);
                    xHandler xHand2 = new xHandler(myChannel);
                    // Code to create / start your threads. 
                    // xHand1 and xHand2 will not use myChannel simultaneously  
                }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code that looks like this: public class A { public void doStuff()
I have code that looks like this: public class Polynomial { List<Term> term =
I have a Java class that looks like this: public class My_ABC { int
I have a .java src file that looks like this: class Test { public
When indenting java code with annotations, vim insists on indenting like this: @Test public
I have a very simple Java code like this. I don't have any idea
I have written my own 2-3-4 tree in java. Currently, my code looks like
I'd like to call a Clojure function from Java code. This question hasn't been
I'm parsing a (not well formed) Apple Plist File with java. My Code looks
I was looking at the source code of java.uti.concurrent.locks.AbstractQueuedSynchronizer and the acquire() method looks

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.