java synchronization

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    java synchronization

    Code:
    synchronized void method(){
     this.x = 100;
     this.y = 200;
    }
    the method is synchronized, means only one thread runs the method fully and returns...now my question when a method is synchronized then instance members(data/method) used inside it, are synchronized or not?
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Not necessarily. Another thread could be running another method which also changes those values. You have not locked on the object itself or its members, just the method.

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      that's what i wanted to know ;)

      Comment

      • Dheeraj Joshi
        Recognized Expert Top Contributor
        • Jul 2009
        • 1129

        #4
        When a thread is executing a synchronized block other threads can not access any methods or members of that class if it is object level lock. Else they can execute.

        Regards
        Dheeraj Joshi

        Comment

        Working...