在调用notifyAll之前获取所有等待对象的线程 [英] Getting all the threads waiting for an object before calling notifyAll

查看:79
本文介绍了在调用notifyAll之前获取所有等待对象的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆代码&处理器很小,
调用notifyAll时,有时会导致性能问题(100毫秒),所以我想知道当前等待对象释放的线程是什么.

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 threads currently waiting for the object to get released.

synchronized (obj){
//do some stuff..
obj.notifyall();
}

我想在调用obj.notifyAll()

推荐答案

是否所有线程都在资源上等待相同的条件?如果是,那么您可以尝试替换obj.notify()而不是obj.notifyAll,尽管实际上不建议这样做. AFAIK,没有办法检索"在给定对象上等待的所有线程的列表(尽管您可以通过编程方式获取进程的线程转储并查看线程,但是我敢肯定那不是您所拥有的)头脑).即使存在,列出线程然后对它们执行操作"也肯定会比花费notifyAll所花费的时间更多.

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.

此外,如果处理器很小",请尝试限制产生的线程数,因为在没有大量实际"线程的情况下,创建太多线程通常是一项开销.这样,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);
    }
}

这篇关于在调用notifyAll之前获取所有等待对象的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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