Getting all the threads waiting for an object before calling notifyAll

前端 未结 2 635
谎友^
谎友^ 2021-01-07 07:13

I have bunch of code & the processor is small,
when calling notifyAll, sometimes creating performance issue (100ms) so I want to know what are the threa

相关标签:
2条回答
  • 2021-01-07 07:44

    You can use Thread.getAllStackTraces() and print out all the threads which are waiting.

    0 讨论(0)
  • 2021-01-07 07:45

    Are all the threads waiting on the resource for the same condition? If yes, then you can try replacing obj.notify() instead of obj.notifyAll though it really isn't recommended. AFAIK, there is no way of "retrieving" a list of all the threads waiting on a given object (though you can programatically get a thread-dump of the process and view the threads, but I'm sure that's not what you had in mind). Even if there was, listing threads and then doing "something" with them would surely take more than the time taken to notifyAll.

    Also, if the "processor is small", try limiting the number of threads spawned since without a fair amount of "real" threads, creating too many threads is typically an overhead. That way, notifyAll wouldn't wake up a bunch of threads.

    Here is a small program which demonstrates dumping of thread state information with comments inlined:

    package net.sanjayts.test;
    
    import java.lang.management.ManagementFactory;
    import java.lang.management.ThreadInfo;
    import java.util.concurrent.TimeUnit;
    
    public class ThreadDumpTest {
    
        public static void main(String[] args) throws Exception {
            final Object lock = new Object();
            for (int i = 0; i < 6; ++i) {
                final int cnt = i;
                new DaemonThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            // If counter is even, try to acquire common lock and then
                            // sleep. If odd, sleep without trying to acquire the lock.
                            // This way, if we do a thread dump, we'll see threads in
                            // different states (TIMED_WAIT for those sleeping threads
                            // and BLOCKED for those waiting for the common "lock".
                            if (cnt % 2 == 0) {
                                synchronized (lock) {
                                    TimeUnit.MINUTES.sleep(1); // sleep 1 min
                                }
                            } else {
                                TimeUnit.MINUTES.sleep(1); // sleep 1 min
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }, "mythread-" + cnt).start();
            }
            ThreadInfo[] infos = ManagementFactory.
                    getThreadMXBean().dumpAllThreads(true, true);
            for (ThreadInfo info : infos) {
                System.out.println(info);
                System.out.println("===========================");
            }
            TimeUnit.SECONDS.sleep(2);
        }
    
    }
    
    class DaemonThread extends Thread {
        public DaemonThread(Runnable r, String name) {
            super(r, name);
            setDaemon(true);
        }
    }
    
    0 讨论(0)
提交回复
热议问题