旋转锁,然后尝试最终阻止爪哇

发布于 2025-02-02 18:02:45 字数 907 浏览 2 评论 0 原文

为什么要释放一个旋转锁? 在 longAdder.class spin锁 cellbusy 最后发布 block中,但是没有资源或语句会在尝试块,因此,在终于块中释放旋转锁定的目的是什么。 我猜想线程要用尽其CPU时间时,JVM将尝试运行 try block中的所有语句,然后在最后在中发布资源,因此 cellbusy 可以在线程释放CPU之前释放锁定。 如果我评论最后,有时似乎有些线程可能会很长。

                  if (casCellsBusy()) { // CAS lock
                     try {
                        if (cells == null) {
                            cells = cs;
                        }
                     } finally {
                        cellsBusy = 0;  // free lock
                     }
                  }


                     if (casCellsBusy()) { // CAS lock
                        if (cells == null) {
                            cells = cs;
                        }
                        cellsBusy = 0;
                     }

Why release a spin lock in finally block?
In LongAdder.class the spin lock cellsBusy is released in finally block, but there are no resources or statements that will throw an exception in try block, so what is the purpose of release a spin lock in finally block.
I guess when a thread is about running out of its cpu time, JVM will try to run all statement in try block and then release resources in finally block, so the cellsBusy lock can be released before a thread release cpu.
If I comment finally, It seems sometimes some threads may hold this lock very long.

                  if (casCellsBusy()) { // CAS lock
                     try {
                        if (cells == null) {
                            cells = cs;
                        }
                     } finally {
                        cellsBusy = 0;  // free lock
                     }
                  }


                     if (casCellsBusy()) { // CAS lock
                        if (cells == null) {
                            cells = cs;
                        }
                        cellsBusy = 0;
                     }

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文