Java中与静态方法有关的同步问题 [英] Synchronization concerns with a static method in java

查看:63
本文介绍了Java中与静态方法有关的同步问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个Utility类,

Suppose I have a Utility class,

public class Utility {

    private Utility() {} //Don't worry, just doing this as guarantee.

    public static int stringToInt(String s) {
        return Integer.parseInt(s);
    }
};

现在,假设在多线程应用程序中,一个线程调用了Utility.stringToInt()方法,并且 操作进入了该方法调用,另一个线程调用了同一方法并传递了另一个s. 在这种情况下会发生什么? Java是否锁定静态方法?

Now, suppose, in a multithreaded application, a thread calls, Utility.stringToInt() method and while the operation enters the method call, another thread calls the same method passing a different s. What happens in this case? Does Java lock a static method?

推荐答案

这里没有问题.每个线程将使用其自己的堆栈,因此不同的s之间没有冲突点.而且Integer.parseInt()是线程安全的,因为它仅使用局部变量.

There is no issue here. Each thread will use its own stack so there is no point of collision among different s. And Integer.parseInt() is thread safe as it only uses local variables.

这篇关于Java中与静态方法有关的同步问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆