-1

I am little confused in understanding the role of 'this' in a synchronized block. I have some basic understanding of what synchronized block is used for. This article says -

Don’t synchronize on the non-final field on synchronized block because the reference to the non-final field may change anytime and then different threads might synchronize on different objects i.e. no synchronization at all.

I am unclear on what does it actually mean. Can someone please guide me in understanding what the this actually is in this context and why non-final fields are not supposed to be synchronized in synchronized block.

Community
  • 1
  • 1
Naxi
  • 1,504
  • 5
  • 33
  • 72
  • 2
    this has only one role within the entire Java world, and that is to point to the current instance of the class you're in, whether it's in a synchronized block or not. – Stultuske Jun 19 '19 at 05:42
  • 2
    A (very extensive) discussion about `synchronized(this)` over at https://stackoverflow.com/questions/442564/avoid-synchronizedthis-in-java – Thilo Jun 19 '19 at 05:47
  • According to my knowledge @Stultuske is correct. In java 'this' keyword always has the same meaning and it always represent the current instance of that class. – Anjali Pavithra Jun 19 '19 at 06:08

2 Answers2

3

this in java are represents the current object or instance like this

class TreeNode {
    int content;
    TreeNode left;
    TreeNode right;

    public TreeNode(int content, TreeNode left, TreeNode right) {
        this.content = content;
        this.left = left;
        this.right = right;
    }

    public void setContent(int content) {
        synchronized (this) {
            this.content = content;
        }
    }
}

In the above code, this always represents the same meaning: the instance itself.

and regarding the article said, my understand is don't do like this:

Class Test{
    Student ken = new Student('ken');

    public void LockTest() {

      synchronized(ken){
        doSomething();
      }
    }
}

because obj is non-final field, it can be changed in somewhere by ken = new Student('adam'); , in this case synchronized may only lock the Student('ken') instance at firstly,

and then reference ken already changed to Student('adam') instance, other thread will find Student('adam') dont locked, so get into the synchronized block.

so there are possibly many different locks if you synchronized a non-final fields.

Wang Kenneth
  • 387
  • 3
  • 7
  • if we use `final Student ken = new Student('ken');` synchronized(ken) will make sure that all threads are try to get the same lock. – Wang Kenneth Jun 19 '19 at 06:26
  • 2
    Imagine a normal toilet, with one door. This is as it should be. If someone wants to use the toilet, you have to wait till it becomes available. Now imagine if whoever wanted to could add their own door to the toilet whenever they wanted. Not much privacy guaranteed with that system, is there? – Amadan Jun 19 '19 at 06:32
  • Thanks Wang. @Amadan, your example is just perfect and that was what I was looking for . :-) – Naxi Jun 19 '19 at 07:02
1

this represents current instance of that class

flakes
  • 21,558
  • 8
  • 41
  • 88
  • To add to the answer; each time you create a `new` instance of a class, *e.g. `new Foo()`*, you create a new instance and are returned a reference to that instance. From within the class *(if you are in an instance scoped block)*, then you always obtain that reference by using `this`. – flakes Jun 19 '19 at 06:08