本文介绍了并发访问实用程序静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个场景,其中多个线程调用静态方法,如下所示:

We have a scenario in which several threads call a static method like the following one:

public static boolean isEmpty(final String s) {
    return s == null || s.length() < 1;
}

如果100个线程调用它会引起不一致的问题吗?

Could it cause a problem of inconsistence if 100 threads call it?

推荐答案

否.这不会遇到任何并发问题.

No. This wont suffer from any concurrency problems.

1)您传入的参数是一个字符串,它是一个不可变的类(其值无法修改)

1) The parameter you pass in is a String which is an immutable class (its value cannot be modified)

2)该方法不会尝试修改任何共享状态

2) The method doesn't try to modify any shared state

这篇关于并发访问实用程序静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 03:49
查看更多