简体   繁体   English

如何测量线程等待数据库连接时出现的延迟?

[英]How to measure latency which appears when threads are awaiting for db connection?

Say I have java application with 10 deamon threads. 说我有10个守护进程线程的Java应用程序。

Say I have connection pool (for example c3p0) with maximum 3 connections. 说我有最多3个连接的连接池(例如c3p0)。

Each of threads is working with database. 每个线程都在使用数据库。 It could happen (ofcourse it really happens) when one thread is waiting for a connection in BLOCKED state. 当一个线程正在等待处于BLOCKED状态的连接时,可能会发生(当然确实会发生)。

And I need to measure total thread latency, when each of them is in BLOCKED state awaiting for the connection. 当它们每个处于等待连接的阻塞状态时,我需要测量总线程延迟。

What is the best way to do that? 最好的方法是什么?

Extend Thread with your own implementation and Override the method "notify" so it is saving "system.currentTimeMilis" in some kind of cache- you can call a method with the miliseconds argument to take care of the notifyTimes for each thread. 使用您自己的实现来扩展Thread并覆盖方法“ notify”,以便将“ system.currentTimeMilis”保存在某种缓存中-您可以调用带有miliseconds参数的方法来照顾每个线程的notifyTimes。 Then in your extended thread overide the run/start method and get again the currentTimeMilis. 然后,在扩展线程中,覆盖run / start方法并再次获取currentTimeMilis。 Get the currentTime of the wait method too (again override and dont forget to call the super method in the overridings). 还要获取wait方法的currentTime(再次重写,并且不要忘记在重写中调用super方法)。 Play with these 3 types of times of your threads and you should be able to do your benchmarks. 玩这三种类型的线程,您应该可以进行基准测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM