• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Synchronise

 
marlajee Borstone
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE Java MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ramesh Kumar Swarnkar
Ranch Hand
Posts: 84
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic