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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:51:56+00:00 2026-05-26T03:51:56+00:00

Why do people synchronize for just 1 line of code? What is there to

  • 0

Why do people “synchronize” for just 1 line of code? What is there to “synchronize”?

public final void addListener(Listener listener) {
  synchronized (listeners) {
    listeners.add(listener);
  }
}

EDIT: Thank you everyone. Very good answers to from all!

  • 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-26T03:51:57+00:00Added an answer on May 26, 2026 at 3:51 am

    synchronized on its own means that if multiple threads try to run this piece of code at the same time, only one of those threads is allowed inside the block at any given time. synchronized (listeners) uses listeners as a lock identifier, which means that this restriction applies to all blocks which synchronize on that variable – if one thread is inside one of those blocks, no other thread may enter any of them.

    Even though there’s only a single function call in a block, this can still make sense: that function consists of a lot of other instructions, and control may switch to a different thread while the first one is in the middle of that function. If the function is not thread-safe, that can cause problems, such as data getting overwritten.

    In this particular case, the function call consists of adding a value to a collection listeners. While it’s not impossible to make a thread-safe collection, most collections are not thread-safe for multiple writers. Thus, in order to ensure the collection does not get messed up, synchronized is needed.

    EDIT: To give an example of how things may get messed up, assume this simplified implementation of add, where length is the number of elements in the items array:

    public void Add(T item) {
      items[length++] = item;
    }
    

    That length++ bit is not atomic; it consists of a read, an increment, and a write, and the thread can get interrupted after any of them. So, let’s rewrite this a bit, to see what’s really happening:

    public void Add(T item) {
      int temp = length;
      length = length + 1;
      items[temp] = item;
    }
    

    Now assume two threads T1 and T2 enter Add at the same time. Here’s one possible set of events:

    T1: int temp = length;
    T2: int temp = length;
    T2: length = length + 1;
    T2: items[temp] = item;
    T1: length = length + 1;
    T1: items[temp] = item;
    

    The problem there is that the same value is used for temp by both threads, so the last thread to leave ends up overwriting the item that the first one put there; and there’s an unassigned item at the very end.

    It also doesn’t help if length represented the next index to be used so we can use a preincrement:

    public void Add(T item) {
      items[++length] = item;
    }
    

    Again, we rewrite this:

    public void Add(T item) {
      length = length + 1;
      items[length] = item;
    }
    

    Now this is a possible sequence of events:

    T1: length = length + 1;
    T2: length = length + 1;
    T2: items[length] = item;
    T1: items[length] = item;
    

    Once again, the last thread ends up overwriting the first, but now the unassigned item is the second-to-last item.

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

Sidebar

Related Questions

People use gdb on and off for debugging, of course there are lots of
people! I've been told to create next code as homework. If you compile it
import java.util.*; import org.directwebremoting.util.Logger; public class People { public People() { people = new
People have been developing own solutions to the following problems: Consistent messaging frameworks for
People keep giving me examples with carp instead of warn. Why? What makes carp
People use frequently something like: <ListBox ItemsSource={Binding ElementName=thisControl, Path=ListIndexes}> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <Label Content={Binding
People always advised me that if I am doing some application that should use
People buy stuff in a web shop. I take their orders and save them
Many people use Mock Objects when they are writing unit tests. What is a
Most people say never throw an exception out of a destructor - doing so

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.