如何检查哪个线程(共 10 个)调用了 Singleton 构造函数?

How do I check which Thread (out of 10 ) called Singleton constructor?

我试图找出(出于好奇)哪个线程实际调用了单例构造函数。好吧,我完全理解每次执行测试程序时线程都会不同,只是想在实用上检查是否相同。我试过

dumpStack() 
printStackTrace()
getStackTrace()

but the does not give any hint of other class (!phew)

然后尝试通过放置 wait() 来停止最后一行的线程,以便使用 jvisualvm 检查线程转储,令人惊讶的是我想要的信息也在那里丢失。

谁能告诉我如何解决这个问题?

单例类,其getInstance()方法从每个线程的run()方法调用(10)。但是只有一个线程调用会运行实际的构造函数。

package creational;

public class SingleTonBillPaugh
{
  private SingleTonBillPaugh instance;

  public static SingleTonBillPaugh getInstance()
  {

    return SingleTonHelper.instance;
  }

  private SingleTonBillPaugh()
  {
    System.out.println("instance is going to be created");
  }

  private static class SingleTonHelper
  {

    private static final SingleTonBillPaugh instance = new SingleTonBillPaugh();
  }
}

测试 class 单例和线程创建。注意:有意添加 obj.wait() 以保持线程处于暂停模式,从而使它们能够在 jvisualvm 中进行审查。

package creational;

import java.util.concurrent.TimeUnit;

public class SingleTonBillPaughTest
{
  public static void main(String[] args)
  {
    Thread[] t = new SingleTonThread[10];
    for (int i = 0; i < 10; i++)
    {
      t[i] = new SingleTonThread();
    }
    for (int i = 0; i < 10; i++)
    {
      t[i].start();
    }
  }
}

class SingleTonThread extends Thread
{
  private Object obj = new Object();

  public void run()
  {
    String threadName = Thread.currentThread().getName();
    System.out.println(threadName);
    try
    {
      TimeUnit.SECONDS.sleep(10);
    }
    catch (InterruptedException e)
    {
      e.printStackTrace();
    }
    System.out.println(threadName + " " + SingleTonBillPaugh.getInstance());
    // Thread.currentThread().dumpStack();
    // new Throwable().printStackTrace();
    // new Throwable().getStackTrace();
    synchronized (obj)
    {
      try
      {
        obj.wait();
      }
      catch (InterruptedException e)
      {
        e.printStackTrace();
      }
    }
  }
}

jvisualvm 的示例输出

2015-01-06 14:22:39
Full thread dump Java HotSpot(TM) 64-Bit Server VM (23.7-b01 mixed mode):

"DestroyJavaVM" prio=6 tid=0x00000000012ed800 nid=0x373c waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
    - None

"Thread-9" prio=6 tid=0x000000000bc22800 nid=0x13b4 in Object.wait() [0x000000000dfaf000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000007d5db3190> (a java.lang.Object)
    at java.lang.Object.wait(Object.java:503)
    at creational.SingleTonThread.run(SingleTonBillPaughTest.java:45)
    - locked <0x00000007d5db3190> (a java.lang.Object)

   Locked ownable synchronizers:
    - None

"Thread-8" prio=6 tid=0x000000000bc21800 nid=0x1b54 in Object.wait() [0x000000000deaf000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000007d5db3028> (a java.lang.Object)
    at java.lang.Object.wait(Object.java:503)
    at creational.SingleTonThread.run(SingleTonBillPaughTest.java:45)
    - locked <0x00000007d5db3028> (a java.lang.Object)

   Locked ownable synchronizers:
    - None

"Thread-7" prio=6 tid=0x000000000bc1e000 nid=0x1128 in Object.wait() [0x000000000ddaf000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000007d5db2ec0> (a java.lang.Object)
    at java.lang.Object.wait(Object.java:503)
    at creational.SingleTonThread.run(SingleTonBillPaughTest.java:45)
    - locked <0x00000007d5db2ec0> (a java.lang.Object)

   Locked ownable synchronizers:
    - None

"Thread-6" prio=6 tid=0x000000000bc19800 nid=0x2f7c in Object.wait() [0x000000000dcaf000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000007d5db2d58> (a java.lang.Object)
    at java.lang.Object.wait(Object.java:503)
    at creational.SingleTonThread.run(SingleTonBillPaughTest.java:45)
    - locked <0x00000007d5db2d58> (a java.lang.Object)

   Locked ownable synchronizers:
    - None

"Thread-5" prio=6 tid=0x000000000bc18800 nid=0x3f30 in Object.wait() [0x000000000dbaf000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000007d5db2bf0> (a java.lang.Object)
    at java.lang.Object.wait(Object.java:503)
    at creational.SingleTonThread.run(SingleTonBillPaughTest.java:45)
    - locked <0x00000007d5db2bf0> (a java.lang.Object)

   Locked ownable synchronizers:
    - None

"Thread-4" prio=6 tid=0x000000000bc18000 nid=0x31c8 in Object.wait() [0x000000000daaf000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000007d5db2a88> (a java.lang.Object)
    at java.lang.Object.wait(Object.java:503)
    at creational.SingleTonThread.run(SingleTonBillPaughTest.java:45)
    - locked <0x00000007d5db2a88> (a java.lang.Object)

   Locked ownable synchronizers:
    - None

"Thread-3" prio=6 tid=0x000000000bc16800 nid=0x41c4 in Object.wait() [0x000000000d9af000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000007d5db2920> (a java.lang.Object)
    at java.lang.Object.wait(Object.java:503)
    at creational.SingleTonThread.run(SingleTonBillPaughTest.java:45)
    - locked <0x00000007d5db2920> (a java.lang.Object)

   Locked ownable synchronizers:
    - None

"Thread-2" prio=6 tid=0x000000000bc10000 nid=0x261c in Object.wait() [0x000000000d8af000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000007d5db27b8> (a java.lang.Object)
    at java.lang.Object.wait(Object.java:503)
    at creational.SingleTonThread.run(SingleTonBillPaughTest.java:45)
    - locked <0x00000007d5db27b8> (a java.lang.Object)

   Locked ownable synchronizers:
    - None

"Thread-1" prio=6 tid=0x000000000bc0f000 nid=0x30dc in Object.wait() [0x000000000d7af000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000007d5db2650> (a java.lang.Object)
    at java.lang.Object.wait(Object.java:503)
    at creational.SingleTonThread.run(SingleTonBillPaughTest.java:45)
    - locked <0x00000007d5db2650> (a java.lang.Object)

   Locked ownable synchronizers:
    - None

"Thread-0" prio=6 tid=0x000000000bc0e000 nid=0x41b4 in Object.wait() [0x000000000d6af000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000007d5db24e8> (a java.lang.Object)
    at java.lang.Object.wait(Object.java:503)
    at creational.SingleTonThread.run(SingleTonBillPaughTest.java:45)
    - locked <0x00000007d5db24e8> (a java.lang.Object)

   Locked ownable synchronizers:
    - None

"Service Thread" daemon prio=6 tid=0x000000000bbe6000 nid=0x3554 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
    - None

"C2 CompilerThread1" daemon prio=10 tid=0x000000000bbe3800 nid=0x2084 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
    - None

"C2 CompilerThread0" daemon prio=10 tid=0x000000000bbde000 nid=0x369c waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
    - None

"Attach Listener" daemon prio=10 tid=0x000000000bbdd000 nid=0x4300 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
    - None

"Signal Dispatcher" daemon prio=10 tid=0x000000000bbd9800 nid=0x3d88 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
    - None

"Finalizer" daemon prio=8 tid=0x000000000bb75000 nid=0x32cc in Object.wait() [0x000000000cfaf000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000007d5d65798> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(Unknown Source)
    - locked <0x00000007d5d65798> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(Unknown Source)
    at java.lang.ref.Finalizer$FinalizerThread.run(Unknown Source)

   Locked ownable synchronizers:
    - None

"Reference Handler" daemon prio=10 tid=0x000000000bb6c000 nid=0x29c8 in Object.wait() [0x000000000ceaf000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000007d5d65320> (a java.lang.ref.Reference$Lock)
    at java.lang.Object.wait(Object.java:503)
    at java.lang.ref.Reference$ReferenceHandler.run(Unknown Source)
    - locked <0x00000007d5d65320> (a java.lang.ref.Reference$Lock)

   Locked ownable synchronizers:
    - None

"VM Thread" prio=10 tid=0x000000000bb62800 nid=0x3cf0 runnable 

"GC task thread#0 (ParallelGC)" prio=6 tid=0x0000000002b9d000 nid=0x3550 runnable 

"GC task thread#1 (ParallelGC)" prio=6 tid=0x0000000002b9e800 nid=0x2e20 runnable 

"GC task thread#2 (ParallelGC)" prio=6 tid=0x0000000002ba0800 nid=0x4254 runnable 

"GC task thread#3 (ParallelGC)" prio=6 tid=0x0000000002ba2000 nid=0x3ddc runnable 

"VM Periodic Task Thread" prio=10 tid=0x000000000bc03000 nid=0x2740 waiting on condition 

JNI global references: 108

只需输入:

String threadName = Thread.currentThread().getName();
System.out.println(threadName);

在构造函数中:

private SingleTonBillPaugh()
{
    String threadName = Thread.currentThread().getName();
    System.out.println(threadName);
}