静态方法中的 Java 数组线程安全吗?

发布于 2024-12-13 15:59:36 字数 562 浏览 1 评论 0原文

   public static int rank(int key, int[] a) {
        int lo = 0;
        int hi = a.length - 1;
        while (lo <= hi) {
            // Key is in a[lo..hi] or not present.
            int mid = lo + (hi - lo) / 2;
            if      (key < a[mid]) hi = mid - 1;
            else if (key > a[mid]) lo = mid + 1;
            else return mid;
        }
        return -1;
    }

上面的静态方法进行二分搜索。它是线程安全的吗?我知道局部变量是线程安全的,但是这里的“a”是一个数组,所以这意味着它是Java中的一个对象,对吧?这是一个问题吗? 该数组只是被读取,没有以任何方式修改,所以我假设这个方法是线程安全的。但我想确保我明白为什么。

谢谢!

   public static int rank(int key, int[] a) {
        int lo = 0;
        int hi = a.length - 1;
        while (lo <= hi) {
            // Key is in a[lo..hi] or not present.
            int mid = lo + (hi - lo) / 2;
            if      (key < a[mid]) hi = mid - 1;
            else if (key > a[mid]) lo = mid + 1;
            else return mid;
        }
        return -1;
    }

The above static method does binary search. Is it thread safe? I know that local variables are thread safe but "a" here is an array, so that means it's an object in Java, right? Is that a problem?
The array is just being read, not modified in any way, so I'm assuming this method is thread-safe. But I want to make sure I understand why.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡笑忘祈一世凡恋 2024-12-20 15:59:36

没有数组通常不是线程安全的。您的代码是否属于这种情况取决于其他线程是否有权访问您传入的数组。因为数组是通过引用传递的,所以其他线程可以访问它们。

如果您仅在单个线程中创建/修改数组,或者传入以线程安全方式复制的副本,那就没问题了。

No arrays are not generally threadsafe. Whether your code is in this case depends on whether other threads have access to the array you passed in. Because arrays are passed by reference, then other threads can have access to them.

If you only create/modify the array in a single thread, or if you pass in a copy that is copied in a threadsafe way it will be fine.

雨的味道风的声音 2024-12-20 15:59:36

该方法本身是线程安全的,因为它只获取参数并读取它们,而不将它们发布到任何其他线程。但这并不意味着您不会遇到线程问题。这完全取决于争论的来源。

如果参数构成线程之间的共享状态,则对该状态的每次访问都应该以某种方式同步。但是你必须在线程之间建立同步策略来保护对此状态的访问。所以这个方法,或者这个方法的调用者,应该确保对状态的访问是线程安全的。如果不知道参数来自哪里,就不可能判断这段代码是否是线程安全的。

The method itself is thread-safe, since it only takes its arguments and reads them, without publishing them to any other thread. But that doesn't mean you couldn't have a threading problem. It all depends on where the arguments come from.

If the arguments constitute shared state between threads, then every access to this state should be synchronized somehow. But you have to establish a synchronization policy between threads to protect the access to this state. So this method, or the caller of this method, should make sure the access to the state is thread-safe. Without knowing where the arguments come from, it's thus impossible to tell if this code is thread-safe or not.

痴梦一场 2024-12-20 15:59:36

是的,它是线程安全的,正如您所说,您只读取数组,唯一可能的麻烦可能是另一个线程在该方法读取数组的同时更新数组

yes, it is thread safe, as you say you only read the array, the only possible trouble can be if an another thread is updating the array the same time as this method reads it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文