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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:11:37+00:00 2026-05-30T00:11:37+00:00

I have a class that starts a few threads. Each thread (extends Thread) calls

  • 0

I have a class that starts a few threads. Each thread (extends Thread) calls a new instance of class WH, class WH has a variable that is to be shared among all threads. So the hierarchy looks like:

class S extends Thread {
....
....
  WH n = new WH(args);
....
....
}

Now class WH has a variable that is to be shared, declared as:

private static volatile Integer size;

One of the functions tries to access size through Synchronized:
Synchronized (size) { // Program gets stuck at this line
 ... stuff ...
}

It gets stuck even if I spawn off just one thread. Any idea why this is happening? (FYI- I do not want to use AtomicInteger based on my design choices)

Thanks

  • 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-30T00:11:39+00:00Added an answer on May 30, 2026 at 12:11 am

    Your problem is that locking on a Non-Final variable reference has useless semantics.

    Anytime you see something doing a synchronized(var); and var is an instance or static variable and isn’t marked final, it is an error, because anything can come along and do a var = new Thing(); and now there are at least 2 threads that can operate on that block at the same time, this is a logical error no exceptions. Every Java lint style checker flags this as a critical error, just because the compiler doesn’t catch this doesn’t mean it has any usefulness in any case.

    In this case, you are exposing these useless semantics by changing the value of the immutable Integer class.

    Your Integer variable size is non-Final and is Immutable which means every time you change it you must change the reference to the new object that represents the new value and every thread will get a new and different reference to lock onto. Thus no locking.

    Use a private static final AtomicInteger size = new AtomicInteger();

    And then you can synchronize(size); since size is now final you can mutate it in place and get the intended and correct semantics.

    or you can synchronize(some_other_final_reference); and use a regular int as long as that reference that is synchronized on is final and can be in scope of any thread that needs to acquire a handle to it, it will work.

    Personally I would use the AtomicInteger it is more cohesive that way, you are locking on what you don’t want changing by any other thread, self-documenting and clear intentions.

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

Sidebar

Related Questions

I'm looking to have a class (subclassed from threading.Thread) that is initialised and started
Imagine I have a class that represents a simple washing machine. It can perform
Possible Duplicate: Locking main() thread Below you'll find my code, Main calls two threads,
I have an application that performs a write to a database each time a
I have a class that is wrapped with swig, and registered with lua. I
I have an application that contains a main Activity, which has a very simple
On ASP.NET MVC 2 I have an ActionFilterAttribute called [Transaction] that starts an NHibernate
I have never had to use threads much - only a few occasions. But
So, I'm pretty new to all this network programming, and I have a few
I have a class that I've created to allow asynchronous sequential execution of tasks,

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.