4
  "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)

线程都是可运行的,并且它们持有相同的锁。两个线程都可以运行时可以锁定相同的地址吗?这是一个JRE错误吗?

4

1 回答 1

11

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

使用以下程序可以轻松重现该行为:

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();
    }
}
于 2013-04-27T09:19:25.667 回答