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

The Archive Base Latest Questions

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

If I am using synchronized , then does the object which is being synchronized

  • 0

If I am using synchronized, then does the object which is being synchronized have to be static?

EDIT:
I meant to say reference to objects must be static. I noticed that one of the examples I was reading stated that in order for threads to use a synchronized method in class A, then the reference to class A must be static.

So, I was wondering whether it is a rule to make the reference to an object static, so that all the threads which call the synchronized method are using the same copy of instance. In my example, the synchronized method is used to count from 1 to 10. So each thread accessing this synchronized method should each count 1 to 10. I tried this without static and the results were incorrect.

  • 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:01:20+00:00Added an answer on June 9, 2026 at 8:01 pm

    At the risk of over-trivializing this:

    1. Examine code and identify the set of resources, or data that will be accessed by code that will be running on concurrent threads. The code to consider may span multiple methods, possibly classes.

    2. In general, synchronize on something representative of the set of resources:

      • If the set of resources are all object instance data (non static), then it’s acceptable to synchronize on the object owning the data. (If that object isn’t ‘this’, be asking yourself many questions!).

      • If any portion of the set of resources is static class data, then you must synchronize on something representative of the static data. This might be the class itself. (Also know that primitive values aren’t objects).

      • ALWAY lock on the same thing for any given set of resources. This ensures that threads competing for the same set of resources are coordinating properly with each other.

    3. If you are considering two such sets of resources, there must not be any one item that belongs to both sets. If such were the case, they must become one single set of resources.

    4. If you have methods in the same object that are NOT competing (read or write) for anything from the set of resources identified in (1), then that method may not need to be synchronized. HOWEVER, if the method will be used concurrently then consider that:

      • If such a method does access data from another set of resources, then it will need to be synchronized to that set.

      • See (3).

    Read this to understand how the sychronized keyword works for static vs instance methods

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

Sidebar

Related Questions

Accidentally I have been using a @synchronized block with the semaphore self in a
I have two NSScrollView's with scroll synchronized with each other (using Apple's example )
I have an object passed into a function which passes a delayed runnable to
Is using the 'synchronized' keyword on methods in a Java DAO going to cause
I'm using PHP, Oracle and crontab. Crontab is invoking a php script, which is
I have an executable that I call using the shell command: Shell (ThisWorkbook.Path &
In my project I have a Thread which might be modiefied by the thread
I have a Queue on which the Enqueue operation would be performed by one
Is it possible to have selective queries in PostgreSQL which select different tables/columns based
Implement using existing Synchronized Java classes (Hashtable, StringBuffer, Vector) or synchronize the blocks when

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.