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.

Richard Sitze
  • 8,262
  • 3
  • 36
  • 48
user547453
  • 1,035
  • 6
  • 22
  • 38

5 Answers5

1

Objects can't be static or non-static: this attribute only applies to variables. All objects are equally living on the heap until they can't be reached through references from the "root" anymore.

For synchronization we need the objects themselves (not the variables that hold references) so it makes no difference if references to those objects are held by static-, non-static- or local variables.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
1

Its not the object that's static or non-static, its the data whose state is being protected from concurrency problems.

1. synchronized is a keyword which is used when 2 or more thread access the same object.

2. Synchronization is about protecting the State of the Crucial Data.

3. Every object has a Lock and a Key, so to protect the Data which is an instance variable, an object's key is used.

4. Similarly to protect the State of the Data which is static, ie (Class Data), class's key is used.

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
1

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

Community
  • 1
  • 1
Richard Sitze
  • 8,262
  • 3
  • 36
  • 48
0

Not necessarily.

If you were building a web app (for example) and each client session was working on a List of objects specific to the session (eg, a list of books for each user) then it's fine for your thread to read your list of books while my thread is updating my list of books.

Nicholas Albion
  • 3,096
  • 7
  • 33
  • 56
0

We don't synchronize an object. We synchronize a block of code (which could be a method or code within a synchronized block), so that only one thread can pass through that block at a time. Object provides the lock, which is used to prevent other threads from entering that block of code if a thread is already running through that block of code. It doesn't matter if the reference to the object is static regarding the thread synchronization, but also note that you cannot use a non-static variable within a static method.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142