On Mon, Apr 21, 2014 at 8:21 PM, Chamil Jeewantha <cha...@wso2.com> wrote:

>  B.staticMethod() is already thread safe (it is synchronized). because of
> that, the caller class (Class A) do not need to use a lock again.
>
> Yes. And this stackoverflaw answer[1] gives a tip
[1]
http://stackoverflow.com/questions/5443297/concurrency-in-java-synchronized-static-methods
On Mon, Apr 21, 2014 at 8:04 PM, Sohani Weerasinghe <soh...@wso2.com> 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
> Dev@wso2.org
> http://wso2.org/cgi-bin/mailman/listinfo/dev
>
>


-- 
K.D. Chamil Jeewantha
Associate Technical Lead
WSO2, Inc.;  http://wso2.com
Mobile: +94716813892


> _______________________________________________
> Dev mailing list
> Dev@wso2.org
> http://wso2.org/cgi-bin/mailman/listinfo/dev
>
>


-- 
*Malintha Adikari*
 Software Engineer
WSO2 Inc.; http://wso2.com
lean.enterprise.middleware

Mobile: +94 71 2312958
Blog:    http://malinthas.blogspot.com
Page:   http://about.me/malintha
 (): උපදිෂ්ට, ද්වෙෂ කරනවා, ද්වේෂ කරනවා, පිළිකුල් කරනවා, x ලකුණ, ලකුණ
යොදනවා, වකුටු, ස්වරිතස්වරය ඇඟවීමට යොදන ලකුණ, යමක් කල යුතුයි
_______________________________________________
Dev mailing list
Dev@wso2.org
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to