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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:46:38+00:00 2026-06-09T20:46:38+00:00

Possible Duplicate: synchronized block vs synchronized method? From accepted answer to this question: In

  • 0

Possible Duplicate:
synchronized block vs synchronized method?

From accepted answer to this question: In Java critical sections, what should I synchronize on?
I learn that

public synchronized void foo() {
    // do something thread-safe
}

and:

public void foo() {
    synchronized (this) {
        // do something thread-safe
    }
}

do exactly the same thing. But in first case we make synchronized only one method of object, and in second case we make inaccessible Whole object. So why this two code snippests do same things?

  • 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-09T20:46:40+00:00Added an answer on June 9, 2026 at 8:46 pm

    You seem to be mixing things.

    Firstly

    public synchronized void method() {
    }
    

    is equivalent, from a synchronization perspective, to:

    public void method() {
        synchronized (this) {
        }
    }
    

    The pros / cons have already been mentioned and the various duplicates give more information.

    Secondly,

    synchronized(someObject) {
        //some instructions
    }
    

    means that the instructions in the synchronized block can’t be executed simultaneously by 2 threads because they need to acquire the monitor on someObject to do so. (That assumes that someObject is a final reference that does not change).

    In your case, someObject happens to be this.

    Any code in your object that is not synchronized, can still be executed concurrently, even if the monitor on this is held by a thread because it is running the synchronized block. In other words, synchronized(this) does NOT “lock the whole object”. It only prevents 2 threads from executing the synchronized block at the same time.

    Finally, if you have two synchronized methods (both using this as a lock), if one thread (T1) acquires a lock on this to execute one of those 2 methods, no other thread is allowed to execute any of the two methods, because they would need to acquire the lock on this, which is already held by T1.

    That situation can create contention in critical sections, in which case a more fine grained locking strategy must be used (for example, using multiple locks).

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

Sidebar

Related Questions

Possible Duplicate: synchronized block vs synchronized method? Hi all I was wondering is Snippet-A
Possible Duplicate: Efficient way to implement singleton pattern in Java I was reading this
Possible Duplicate: how to use foursquare API in android application? This is a question
Possible Duplicate: How do synchronized static methods work in Java? I was wondering what
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: Threads synchronization. How exactly lock makes access to memory 'correct'? This question
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: php == vs === operator Reference - What does this symbol mean
Possible Duplicate: How to call a JavaScript function from PHP? I have a php

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.