多个线程持有相同的锁? [英] Multiple threads hold the same lock?

查看:142
本文介绍了多个线程持有相同的锁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  "dashboardRefreshContainer-8" - Thread t@1384
   java.lang.Thread.State: RUNNABLE
    at sun.util.calendar.ZoneInfo.getLastRule(ZoneInfo.java:638)
    - locked <4d70153e> (a sun.util.calendar.ZoneInfo)
    at sun.util.calendar.ZoneInfo.getOffsets(ZoneInfo.java:275)
    at sun.util.calendar.ZoneInfo.getOffsets(ZoneInfo.java:225)
    at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2024)
    at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:1996)
    at java.util.Calendar.setTimeInMillis(Calendar.java:1109)
    at java.util.Calendar.setTime(Calendar.java:1075)

"TP-Processor38" - Thread t@158
   java.lang.Thread.State: RUNNABLE
    at sun.util.calendar.ZoneInfo.getLastRule(ZoneInfo.java:638)
    - locked <4d70153e> (a sun.util.calendar.ZoneInfo)
    at sun.util.calendar.ZoneInfo.getOffsets(ZoneInfo.java:275)
    at sun.util.calendar.ZoneInfo.getOffsets(ZoneInfo.java:225)
    at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2024)
    at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:1996)
    at java.util.Calendar.setTimeInMillis(Calendar.java:1109)
    at java.util.Calendar.setTime(Calendar.java:1075)

线程都是Runnable,它们持有相同的锁。
两个线程都可以锁定同一个地址,而它们都是RUNNABLE吗?这是一个JRE错误吗?

The threads are both Runnable, and they hold the same lock. Can both threads lock the same address while they are both RUNNABLE? Is that a JRE bug?

推荐答案

问题仅存在于线程转储中。实际上,在任何时间点,锁只由一个线程保持。但是,线程转储显示两个具有相同锁定的不同线程,因为它不是原子的。

The problem exists only in the thread dump. In fact, at any point in time, the lock is held by only one thread. However, the thread dump shows two different threads with the same lock, because it is not atomic.

使用以下程序可以轻松复制该行为:

The behavior can easily reproduced with the following program:

public class Test {
    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            public void run() {
                for (;;) {
                    synchronized (this) { }
                }
            }
        };
        new Thread(runnable).start();
        new Thread(runnable).start();
    }
}

这篇关于多个线程持有相同的锁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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