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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:13:56+00:00 2026-05-11T00:13:56+00:00

In Java, the idiomatic way to declare critical sections in the code is the

  • 0

In Java, the idiomatic way to declare critical sections in the code is the following:

private void doSomething() {   // thread-safe code   synchronized(this) {     // thread-unsafe code   }   // thread-safe code } 

Almost all blocks synchronize on this, but is there a particular reason for this? Are there other possibilities? Are there any best practices on what object to synchronize on? (such as private instances of Object?)

  • 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. 2026-05-11T00:13:57+00:00Added an answer on May 11, 2026 at 12:13 am

    First, note that the following code snippets are identical.

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

    and:

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

    do exactly the same thing. No preference for either one of them except for code readability and style.

    When you do synchronize methods or blocks of code, it’s important to know why you are doing such a thing, and what object exactly you are locking, and for what purpose.

    Also note that there are situations in which you will want to client-side synchronize blocks of code in which the monitor you are asking for (i.e. the synchronized object) is not necessarily this, like in this example :

    Vector v = getSomeGlobalVector(); synchronized (v) {     // some thread-safe operation on the vector } 

    I suggest you get more knowledge about concurrent programming, it will serve you a great deal once you know exactly what’s happening behind the scenes. You should check out Concurrent Programming in Java, a great book on the subject. If you want a quick dive-in to the subject, check out Java Concurrency @ Sun

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

Sidebar

Ask A Question

Stats

  • Questions 77k
  • Answers 77k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You can do it by subclassing the GroovyClassLoader and enforcing… May 11, 2026 at 3:29 pm
  • added an answer I'm answering my own question in case someone finds this… May 11, 2026 at 3:28 pm
  • added an answer According to the ICU (International Components for Unicode), Java 6… May 11, 2026 at 3:28 pm

Related Questions

I've got a highly multithreaded app written in Ruby that shares a few instance
I am extending a class defined in a library which I cannot change: public
I'm working with a map in Scala and doing the usual if there's no
I have developed some custom DAO-like classes to meet some very specialized requirements for

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.