简体   繁体   中英

Java static methods with class level variables

I have static method when multiple threads are accessing , will the data updated incorrectly,

public class A
{

    private static B b=null;

    public static B create()
    {
        b= new B();
        return b;
    }

    public static B process()
    {
        // doing some processing with b;
        return b;
    }
}

If multiple threads are accessing simultaneously, Will B get affected?

如果要保护该值并确保正确更新该值,则应使该方法同步。

否,因为b存在于对象级别,并且不能静态访问。

No one can tell until we see the way you access and work on b .

However, one thing I can tell is, static or not is usually not a main factor that affect thread-safetiness.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM