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

  • Home
  • SEARCH
  • 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 8974921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:46:02+00:00 2026-06-15T18:46:02+00:00

Please, I have doubts between a synchronized block and a synchronized method with the

  • 0

Please, I have doubts between a synchronized block and a synchronized method with the below scenario:

Part1
————–:

Class table
{
    Synchronized(this)
    {
        ……………………
    }
}

Here we got lock on object of table class.
————————————–…

Part2:

Class table
{
    Customer cust=new Customer();
    Synchronized(cust)
    {
        ……………………
    }
}

Here we got lock on object of customer.i.e on cust
In part1 we got lock on object of?- table class.
We get lock on table class because object of table class may try to access the synchronized
block from different places simultaneously.To prevent it we got lock on object of table class
in part1.
Now come to part2:?-
Here we got lock on object of customer class
————————————–…

My doubts:

  1. why should we lock an object in table class other than table class in part2?

  2. If we do not get lock on cust in part 2 then we can access synchronized block using table
    class
    If we lock cust then also we need object of table class to access synchronized block.
    Object of table class only needed to access synchronized block.
    Due to synchronization we prevented object of table class to access synchronized block
    simultaneously from different threads.
    Then why should we lock object other than table class in table class?

  3. Will object of customer class try to access the synchronized block simultaneously from
    different places?

  4. In part2 we got lock on object other than table class.
    In What situations getting lock in table class on an object of a class other than table is
    benefit of getting lock on object of customer class in part2?

  5. What is the need to lock object of customer class i.e other than table class?

  6. What damage will happen if we do not get lock on object of customer class in part2?

Please, I am new to Java thread and I have been trying to wrap my head around those questions for a while. Any clear explanation will help alot. 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-06-15T18:46:03+00:00Added an answer on June 15, 2026 at 6:46 pm

    First thing you should check out is :
    What kind of DATA fields will be protected using “synchronized” keyword for different types.

    static variables/methods are class level objects and all other variables/methods are instance level.

    A class level lock is the lock which makes all object of a class to wait until the corresponding lock is not released.

    e.g.

    Class A{
    static synchronized void foo(){}
    }
    

    Here the method foo is synchronized and hence all the threads on all the objects of the class will wait until the object currently running the foo method completes its execution.

    Similarly an instance level lock makes all the threads started using the instance of the class to wait until the lock is not released.

    e.g.

    Class A{
    synchronized void bar(){}
    }
    

    Here all the threads started from the object which is currently executing the bar method will wait until the current threads completes its execution. Note that other threads of other objects can execute the bar method while another object’s thread is executing the bar method.

    In other words, a thread cannot enter a synchronized block of code until it holds
    the appropriate object lock.

    For instance methods, this is the lock of an instance.

    For static methods this is the Class object lock.

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

Sidebar

Related Questions

I have two doubts, please help me on this: Is it possible to define
I have doubt with android backstack , please consider the scenario I have two
I have a long standing doubt. Could someone please tell me whether method overloading
please have a look at the following code import java.util.ArrayList; import java.util.List; public class
Please have a look at the following code namespace Funny { class QuesionsAndAnswers {
Please have a look at the following code: $(#saveButton).click(function(){ $this = $(#tableData).find(input:checked).parent().parent(); tea =
Please have a look at the following machine code ‎0111001101110100011100100110010101110011011100110110010101100100 This means something. I
Please have a look at this form that I am trying to design with
Please have a look at the code below: import string from collections import defaultdict
Please have a look at this page www.pixeli.ca/issue. I have begun making a page

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.