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 Static Synchronise Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Static Synchronise" Watch "Static Synchronise" New topic
Author

Static Synchronise

marlajee Borstone
Ranch Hand

Joined: Jun 26, 2008
Posts: 35
hi,
I have a small doubt on static synchronise.
Say I have a class 'A' that has three methods:

These three methods of class 'A' are tried to be accessed by four different threads t1, t2, t3 and t4 from some third class 'B'; like:
Can threads t1 and t2 access the single static method meth1();while t3
trying to acess meth2() and t4 an instance method, meth3(). Can all these four threads execute concurrently as per the statement written in above class 'B'?
[ October 14, 2008: Message edited by: marlajee Borstone ]
Ramesh Kumar Swarnkar
Ranch Hand

Joined: Sep 15, 2003
Posts: 84
thread t1 and t2 can not execute the same method meth1() as this is static method.To work on this method one need to aquire a class level lock. Here it is already auquired by t1. Unless finishes the execution t2 can not work on it concurrently. However, t3 can access meth2() and t4 can acess meth3() concurrently. Reason it: t3 is accessing a synchronise method, which is possible as because the Object level luck is free and is not acquired by any thread. Hence thread t3 can access meth2(). And meth4() is instance method, it can be accessed by number of thread concurrently.

So if we see the execution of all these threads at any point of time, it will be


regards,
Ramesh
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

No two threads can concurrently execute the method synchronized on the same monitor.
The monitor used for static methods is different(class member variable) than the one used to non-static(this variable)

In your case,
More than one thread can not execute method1 concurrently.
More than one thread can not execute method2 concurrently
Two threads *can* concurrently execute method1 and method2
Any number of threads can execute method3 irrespective of how many threads are executing method1 and method2.


apigee, a better way to API!
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Originally posted by Ramesh Kumar Swarnkar:


Hi Ramesh, it is not invalid or not allowed to call t2.meth1(). It is quite valid. You would just expect the execution of t2 to delay until the execution of t1 completes.


Steve
Ramesh Kumar Swarnkar
Ranch Hand

Joined: Sep 15, 2003
Posts: 84
Hi Steve,
I mean the same thing, that is why I had mentioned :
So if we see the execution of all these threads at any point of time, it will be

But, thanks for making it more precise.

regards,
Ramesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static Synchronise