You can do it simply lock the class

synchronized(B.class){
    B.staticMethod("hello");
}
On Apr 21, 2014 8:06 PM, "Sohani Weerasinghe" <[email protected]> wrote:

> Hi All,
>
> Since synchronization on static methods is a class level locking a second
> thread can't access the method until first thread releases the lock. The
> static method has synchronized as shown below.
>
> public class B{
> public *synchronized* static void staticMethod(String a){
>     }
> }
>
> If this method calls by another class (Class A), then do I have to use a
> synchronized block as shown below to avoid multiple threads accessing the
> static method?
>
> Public class A {
> *private static Object lock = new Object();*
>
> public static void main(String[] args){
>     *synchronized(lock){*
>            B.staticMethod("hello");
>                         *}*
>                }
>       }
>
> Can someone provide a solution on this ?
>
> Regards,
> Sohani
>
>  Sohani Weerasinghe
> Software Engineer
> WSO2, Inc: http://wso2.com
>
> Mobile  : +94 716439774
> Blog     :http://christinetechtips.blogspot.com/
> Twitter  : https://twitter.com/sohanichristine
>
> _______________________________________________
> Dev mailing list
> [email protected]
> http://wso2.org/cgi-bin/mailman/listinfo/dev
>
>
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to