File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Thread Synchrinization 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 » Java » Threads and Synchronization
Bookmark "Thread Synchrinization" Watch "Thread Synchrinization" New topic
Author

Thread Synchrinization

Rishi Singh
Ranch Hand

Joined: Dec 09, 2000
Posts: 321
This question is to mahaanna any anyone can contribute to it.I tried the code below and got the output as
i=0Thread-1Static Sysnc print
i=1Thread-1Static Sysnc print
i=2Thread-1Static Sysnc print
i=3Thread-1Static Sysnc print
i=4Thread-1Static Sysnc print
i=0Thread-2 Sysnc print
i=1Thread-2 Sysnc print
i=2Thread-2 Sysnc print
i=3Thread-2 Sysnc print
i=4Thread-2 Sysnc print
THE CODE:

Now the querry is in her post mahaanna has written, if one Thread executes the static Synchronized method on a instance of a class it means it has its class lock and no other Thread can possibly execute any method on that object, or any other objectof that class.Then how come the above output.Is there something to do with the scheduler,or the inference can be that class lock exists independent of the object lock? Mahaanna please help?

[This message has been edited by Rahul Mahindrakar (edited December 20, 2000).]
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1844
Rishi,
There are two types of locks
One the Object lock
Second the Class lock
The Object lock is associated with the particular Object instance. There is only one lock per Object instance. An Object lock is entered when the thread enters an instance method or a "synchronized(Object o){ }" Block.
The Class also has a lock. This lock is associated with a particular Class. There is only one lock per Class. the Class lock is entered when a Thread enters an static method or a "synchronized(X.class) { }" block.
These two locks are independent of each other. Thus while a particular Thread might own a Class lock another Thread might own a instance/Object lock of the same Class. maha anna has tried to demonstrate this fact by the example given. I give a similar example.
Class Test6 is a class with a static method x and an instance method y. Both these methods are synchronized. There are two Classes x and y which extend Thread. In the run() method of x i call the static method x of Class Test6 10 times. In the run() method of y i call the instance method y() of Test6 10 times. If there was only one lock associated with both the Object and the Class then there should be either an output of "in x" followed by "in y" or vice versa.
However this is not so as can be seen by the output.


------------------
Regds.
Rahul P. Mahindrakar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Synchrinization
 
Similar Threads
synchronized mess up
synch block again
Threads
Thread Synchronization
Synchronized block