File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About Synchronization concept of static and non static method using Class level lock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About Synchronization concept of static and non static method using Class level lock" Watch "About Synchronization concept of static and non static method using Class level lock" New topic
Author

About Synchronization concept of static and non static method using Class level lock

Ritesh Somani
Greenhorn

Joined: May 04, 2012
Posts: 4
Please go through from following code and help me out in Synchronization concept.
Q1. from Class level lock can we able to call non static synchronized method simultaneously ?
Q2. If more than 1 threads are running then they can operate on same static synchronized method simultaneously ?
Q3. From class level lock can more than 1 threads able to execute non static synchronized method ?
Please give me reply......


Thanks & Regards
-Ritesh R Somani
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40


Please QuoteYourSources.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Basically, synchronizing a non-static method means synchronize(this) where this is the instance that calls the method. That means this instance is locked by one thread at a time.
Synchronizing a static method means synchronizied (ThreadDemo8.class), or any other class object. That means this class object is locked by one thread at a time.
One thread can lock the class object while another thread can lock the instance.

So, if two threads access the same object such as ThreadDemo8, the non-static syn method and static syn method can be accessed concurrently by these two threads. One thread runs on the non-static method while another thread runs on the static method.

But the same non-static method or the same static method cannot be accessed concurrently. For example, one thread runs m1(), another thread cannot run m1() at the same time.

Correct me if I am wrong.
Ritesh Somani
Greenhorn

Joined: May 04, 2012
Posts: 4
Thanks a lot for helping me out...!

Good Day

Thanks and Regards
-Ritesh Somani
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About Synchronization concept of static and non static method using Class level lock
 
Similar Threads
wait() releases all locks?
Question about wait() and notify()
help regarding synchronized methods
Static and non-static synchronized method
Basic thread doubt