Bla*_*ond 2 java multithreading
我有大量的代码和处理器很小,
在调用时notifyAll,有时会产生性能问题(100ms)所以我想知道当前等待对象被释放的线程是什么.
synchronized (obj){
//do some stuff..
obj.notifyall();
}
Run Code Online (Sandbox Code Playgroud)
我想在调用之前打印等待对象释放的所有线程 obj.notifyAll()
是否所有线程都在资源上等待相同的条件?如果是,那么你可以尝试替换,obj.notify()而不是obj.notifyAll真的不推荐.AFAIK,没有办法"检索"等待给定对象的所有线程的列表(尽管你可以以编程方式获取进程的线程转储并查看线程,但我确信这不是你所拥有的心神).即使有,列出线程,然后与他们做"某事"肯定会花费更多的时间notifyAll.
此外,如果"处理器很小",请尝试限制产生的线程数,因为没有相当数量的"真实"线程,创建太多线程通常是开销.这样,notifyAll就不会唤醒一堆线程.
这是一个小程序,它演示了内联注释的线程状态信息的转储:
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);
}
}
Run Code Online (Sandbox Code Playgroud)