This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Static and Non-static Synchronized Methods never block each other  RSS feed

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can some one please explian the below sentence with a clear example?

A Static synchronized method (Locks on class instance) and a non-static suynchronised method (Locks on this instance) never block each other.

Thanks in advance.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Priti Manas,


A Static synchronized method (Locks on class instance) and a non-static suynchronised method (Locks on this instance) never block each other



Before we understand this, we know that threads invoking a synchronized non-static method on two different instances, shall not block each other. This is because, each object has its own copy of vulnerable data and the chance of having that data corrupted is negative, if the same data is not being accessed by two concurrent threads. This is because, threads acquire locks on two different objects (This is observable, on two different objects)

Applying the same principle to our problem now. When we invoke a synchronized static method, that thread acquires the lock at class level, actually on a Class instance ( For any class we define in java, there would a corresponding singleton object representing that class.) Threads acquire lock on that object. Meanwhile if any other thread invokes a synchronized Non-static method, this time it holds the lock at object level and actually on the instance of the class.This would be a different object. So two threads on two different objects would not block each other.

Hope I haven't confused you more
 
priti manas duddilla
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So that means,
Locking on a class has no effect on locking on the object belonging to same class.Both are two different locks.
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by priti manas duddilla:
So that means,
Locking on a class has no effect on locking on the object belonging to same class.Both are two different locks.



Yes both are different locks, one "Class" instance the other "this" instance.
[ September 23, 2007: Message edited by: Ahmed Yehia ]
 
priti manas duddilla
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That clears my doubt.Thanks every one!
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!