This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
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 ]
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
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.