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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:54:01+00:00 2026-06-11T09:54:01+00:00

Possible Duplicate: synchronized block vs synchronized method? If anyone can help me with real

  • 0

Possible Duplicate:
synchronized block vs synchronized method?

If anyone can help me with real example about what is different between method synchronized vs object synchronized?, it would be nice.

Method Synchronized Example

public class MyClassExample {
private int i;
public synchronized void increment(){
    i = i + 1;
}
}

Object Synchronized example

public class MyClassExample {
private int i;
Object writeLock = new Object();
 public void increment(){
    synchronized(writeLock) {
        i = i + 1;
    }
}
}
  • 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-11T09:54:03+00:00Added an answer on June 11, 2026 at 9:54 am

    tl;dr – external synchronization opens you up to attack (intentional or otherwise), and also forces you to lock checking that might not be necessary. Also there’s nerdy-good information in at the very bottom of this answer.

    A synchronized method is almost identical (see bottom) to synchronizing on this:

    synchroinzed void foo() {
    
    }
    
    void foo() {
        synchronized(this) {
    
        }
    }
    

    By making the method itself not synchronized, you allow yourself to lock on any Object, not just this. I personally would recommend synchronizing on an internal Object, like so

    private final Object foolock = new Object();
    
    void foo() {
        synchronzied(foolock) {
    
        }
    }
    

    The reason is, if you do a synchronized method, which effectively locks this someone else could synchronize on you and lock you out of your Object! Imagine the following:

    class FooDoer {
        // removed! using synchronized methods instead
        //final Object foolock = new Object();
    
        synchronized void foo() {
    
        }
    }
    
    // thread 1 - attacker
    FooDoer f = new FooDoer();
    globalMap.put("TheFoo",f);
    synchronized(f) {
        while(true); // haha!
    }
    
    // thread 2 - victim
    FooDoer f = globalMap.get("TheFoo");
    f.foo(); // locked, because Thread 1 has locked us out!
    

    It opens yourself up to a denial of service attack. That’s not good! By making the lock internal, you as the author of class get to control exactly who can lock what areas of your object and under what terms.

    The other issue is that you might not have protected data for checking. For example:

    synchronized void foo() {
        if(expensiveAccessCheck()) {
            update();
        }
    }
    
    void foo() {
        if(expensiveAccessCheck()) {
            synchronized(foolock) {
                update();
            }
        }
    }
    

    In this scenario, you don’t have to make everyone else wait while you sit there spinning your wheels. Perhaps you’re scraping data off a URL and then updating. Why make everyone else stay locked out of the data? The lower granularity is better in this case.

    Now you may recall I said almost identical earlier. There is a tiny, tiny, tiny difference between the two. A synchronized method will bake the instruction to synchronize right into the method signature in the bytecode. This will make the bytecode 1 byte shorter, because it doesn’t need to make the extra call. This might have a small impact because the number of bytes in the method’s bytecode is one of the factors in determining whether or not to inline. In spite of this minutia, I highly recommend treating them as the same because this is a micro-optimization that will hardly ever play out as significant in a production setting. It just wouldn’t be complete to call it identical when they aren’t.

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

Sidebar

Related Questions

Possible Duplicate: Direct casting vs 'as' operator? Anyone can give a comparison between as
Possible Duplicate: synchronized block vs synchronized method? From accepted answer to this question: In
Possible Duplicate: synchronized block vs synchronized method? Hi all I was wondering is Snippet-A
Possible Duplicate: Hibernate: different object with the same identifier value was already associated with
Possible Duplicate: C# driver development? I would like to know if I can do
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
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: How can I combine multiple rows into a comma-delimited list in Oracle?
Possible Duplicate: What is the difference between a function expression vs declaration in JavaScript?

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.