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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:25:17+00:00 2026-05-20T20:25:17+00:00

When we say we lock on an object using the synchronized keyword, does it

  • 0

When we say we lock on an object using the synchronized keyword, does it mean we are acquiring a lock on the whole object or only at the code that exists in the block?

In the following example listOne.add is synchronized, does it mean if another thread accesses listOne.get it would be blocked until the first thread gets out of this block? What if a second thread accesses the listTwo.get or listTwo.add methods on the instance variables of the same object when the first thread is still in the synchronized block?

List<String> listONe = new ArrayList<String>();
List<String> listTwo = new ArrayList<String>();

/* ... ... ... */

synchronized(this) {
    listOne.add(something);
}
  • 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-20T20:25:18+00:00Added an answer on May 20, 2026 at 8:25 pm

    Given the methods:

      public void a(String s) {
        synchronized(this) {
          listOne.add(s);
        }
      }
    
      public void b(String s) {
        synchronized(this) {
          listTwo.add(s);
        }
      }
    
      public void c(String s) {
          listOne.add(s);
      }
    
      public void d(String s) {
          synchronized(listOne) {
            listOne.add(s);
          }
      }
    

    You can not call a and b at the same time, as they are locked on the same lock.
    You can however call a and c at the same time (with multiple threads obviously) as they are not locked on the same lock. This can lead to trouble with listOne.

    You can also call a and d at the same time, as d is no different in this context from c. It does not use the same lock.

    It is important that you always lock listOne with the same lock, and allow no access to it without a lock. If listOne and listTwo are somehow related and sometimes need updates at the same time / atomically you’d need one lock for access to both of them. Otherwise 2 separate locks may be better.

    Of course, you’d probably use the relatively new java.util.concurrent classes if all you need is a concurrent list 🙂

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

Sidebar

Related Questions

My main question is does the Threading lock object create atomic locks? It doesn't
Let's say I have a business object that is very expensive to instantiate, and
Let's say I have 2 threads, t1 and t2 , and a lock object,
Let's say I have the following code static class ... { static object myobj
I have a general question regarding synchronized List. Lets say that in the constructor
The documentation for properties in Obj-C 2.0 say that atomic properties use a lock
Say I replace a function by creating a shared object and using LD_PRELOAD to
Let's say I have a code snippet like this synchronized(obj) { do something; }
How do I lock an object in Python? Say I have: class Foo: def
Say I have a SSI script that uses exec, or a PHP script that

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.