Welcome Kapil to this forum 1.What is actually a class lock? I suppose if a thread is executing a static synchronized method, it gets the class lock. So does this mean that if a thread gets a class lock, no other thread can execute any synchronized method on any object of that lock? 2.CardLayout is not in SCJP exam,can you confirm.In RHE CardLayout is also explained. Thanx in advance
Hello Leena, Just like shared methods and objects classes also have a monitor or a semaphore. So when a thread is invoking a static synchtonized method it acquires the monitor of that class and hence locks the class. Thus , other threads can not access this particular class. The non synchronized methods though can be invoked by other threads. If u refer Khalid Mughal's book on (page 281 - first para) he clearly has mentioned that "A thread acquiring the monitor of a class to execute a static synchronized mrthod has no bearing on any thread acquiring the monitor on any object of the class to execute a sunchronized instance method" Correct me if i am wrong.
posted 17 years ago
plase ommit the statement - "Thus , other threads can not access this particular class. " from my earlier reply. Sorry for the incorrect statement
Tejas, Class locks/monitors has nuthing to do with the object lock A thread can have an object's lock and work on synchronized instance method, while another thread work on synchronized static class method with a class montior In a word, they are both independent of each other Ragu