如何检查有多少线程正在等待同步方法解锁
有什么方法可以检查有多少线程正在等待同步方法解锁?
我想知道线程何时调用同步方法:
1)有多少线程已经在等待调用该方法?
2)一旦调用该方法,需要等待该方法解锁多长时间?
解决方案: 我使用堆垛机答案解决了这个问题:
public class LockedClass {
public static int count;
public static void measuringClass() throws IOException{
long startTime = System.currentTimeMillis();
count++;
System.out.println("Threads waiting="+count);
lockedMethod(startTime);
count--;
System.out.println("Threads waiting="+count);
}
public static synchronized void lockedMethod(long startTime) throws IOException{
System.out.println("I spent="+(System.currentTimeMillis()-startTime)+" in the queue");
Hashtable<String, String> params = new Hashtable<String, String>();
params.put("param1", "test");
params.put("param2", "12345678");
String sessionId = Common.getSession(Common.executeHttpRequest(params));
}
}
Is there any way to check how many threads are waiting for a synchronized method to become unlocked?
I would like to know when the thread calls a synchronized method:
1) How many threads are already waiting to call the method?
2) Once the method is called how long it needed to wait for the method to become unlocked?
Solution:
I solved this using stackers answer:
public class LockedClass {
public static int count;
public static void measuringClass() throws IOException{
long startTime = System.currentTimeMillis();
count++;
System.out.println("Threads waiting="+count);
lockedMethod(startTime);
count--;
System.out.println("Threads waiting="+count);
}
public static synchronized void lockedMethod(long startTime) throws IOException{
System.out.println("I spent="+(System.currentTimeMillis()-startTime)+" in the queue");
Hashtable<String, String> params = new Hashtable<String, String>();
params.put("param1", "test");
params.put("param2", "12345678");
String sessionId = Common.getSession(Common.executeHttpRequest(params));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
1)我认为您的代码不可能达到这种级别的可见性。 Java提供了更加强大的并发 API 提供更直接的控制和可见性。作为起点,有一个 Semaphore 类,它有一个方法 getQueueLength() 这听起来可能就是你想要的。
2)当调用同步方法时,调用线程将等待直到该方法被解锁,需要多长时间取决于带有锁的代码执行其操作所需的时间。当对对象进行 wait() 时,您可以指定超时。我不相信你可以用同步方法做到这一点。
1) I do not believe it's possible to have this level of visibility in your code. Java provides a more powerful concurrency API which provides much more direct control and visibility. As a starting point, there's a class Semaphore which has a method getQueueLength() which sounds like it might be what you want.
2) When a synchronized method is called, the calling thread will wait until the method is unlocked, how long that takes depends on how long the code with the lock takes to do its thing. When wait()-ing on an object, you can specify a timeout. I don't believe you can do that with a synchronized method.
您可以将代码转换为使用同步块而不是同步方法,这是我的草稿。我不确定它是否符合您的第二个要求(由于我的英语不稳定)
You could transform your code to use a synchronized block instead of synchronized methods, here is my rough draft. I'm not sure whether it matches you second requirement (due to my choppy english)
我认为 Java Thread Validator 可能会为这些问题提供一些见解。
它没有给出确切的统计数据,但它确实告诉您争用发生的频率以及等待时间等。
I think Java Thread Validator may provide some insight into these questions.
It doesn't give exactly those stats, but it does tell you how often contention happens and what the wait time is etc.