- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试编写一个线程来监控阻塞操作需要多长时间。例如,我有一个这样的阻塞操作:
class BlockingThread extends Thread{
public volatile boolean success = false;
public volatile long startedTime = 0;
public void run(){
startedTime = System.currentTimeMillis();
success = doBlockingAction(); //no idea how long this action will take
}
}
我想要另一个线程,如果阻塞操作花费的时间太长,它基本上会调用“超时”函数:
class MonitorThread extends Thread{
public void run(){
while(System.currentTimeMillis() - blockingThread.startedTime > TIMEOUT_DURATION)
{
..keep waiting until the time runs out..
}
if(!blockingThread.success){
listener.timeout();
//Took too long.
}
}
}
当我在 MonitorThread 中测量时间时,我无法理解如何确保 BlockingThread 实际上当前处于阻塞操作。
如果我这样做,
Thread blockingThread = new BlockingThread();
blockingThread.start();
Thread monitorThread = new MonitorThread();
monitorThread.start();
无法保证其中一个线程实际上会先于另一个线程开始运行代码,因此我目前无法知道我的超时线程是否真的在正确测量阻塞操作的时间。我认为答案与锁定和等待
有关,但我想不通。
最佳答案
我可以建议您使用类 java.util.concurrent.ThreadPoolExecutor
重写代码.这个类有很好的方法 awaitTermination()
,我想这正是您需要的。
编辑:
这是你的 BlockingThread,运行 10 秒,Monitor,等待 5 秒:
package sample.threadexecutor;
import java.text.SimpleDateFormat;
import java.util.Date;
public class BlockingThread implements Runnable{
public boolean succsess = false;
@Override
public void run() {
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss.SSS");
System.out.println(df.format(new Date()) + " start");
try {
Thread.sleep(10000L);
} catch (InterruptedException e) {
System.out.println(df.format(new Date()) + " interrupted");
succsess = false;
return;
}
System.out.println(df.format(new Date()) + " end");
succsess = true;
}
}
和执行器的主要功能:
package sample.threadexecutor;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class main {
public static void main(String[] args) {
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss.SSS");
ExecutorService service= Executors.newSingleThreadExecutor();
service.submit(new BlockingThread());
try {
service.shutdown();
if(service.awaitTermination(5, TimeUnit.SECONDS)){
System.out.println(df.format(new Date()) + " execution completed");
}else{
System.out.println(df.format(new Date()) + " execution timeout");
}
} catch (InterruptedException e) {
System.out.println(df.format(new Date()) + " monitoring interrupted");
}
}
}
和输出:
22:28:37.005 start
22:28:42.006 execution timeout
22:28:47.006 end
如果我们将超时设置为 20 秒,则输出:
22:30:20.210 start
22:30:30.213 end
22:30:30.214 execution completed
关于java - 监视阻塞操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12207495/
我需要记录在网页上执行的事件。 例如。填写登记表。 记录器应捕获关键字条目和在页面上执行的点击,并在请求时回放。同时记录器还应该捕获生成事件的实际元素。说当我在 firstName 中键入 记录器应
我是一个 Jest 新手,我正在为我的 React 应用程序编写单元测试,该应用程序使用 redux 并使用 Typescript 编写。 我的容器组件包含这段代码: const mapDispatc
我想将一些批处理类型的作业从 cron 转移到 Monit,但正在努力让它们正常工作。这些脚本通常每天运行一次,但有时必须在当天晚些时候重新运行。目标是利用 monit 和 m/monit 前端重新运
我正在尝试测试一个组件调用 detectChanges上面注入(inject)了ChangeDetectorRef 我已经逐步完成了代码,它肯定被调用了,但似乎我在组件和测试中得到了不同的 Chang
我想知道是否有一种很好的方法来监视 SharePoint 上的文档库的更改(添加新文件、更改/ checkin 文件、删除文件等) 基本上,什么System.IO.FileSystemWatcher在
是否可以监视 R 正在使用或已用于调用函数的内存量?例如,我有一个任意函数,例如: smallest.sv <- function(){ A <- matrix(rnorm(1e6), 1e3);
这是一个简单的问题,但令人费解...... Azure 服务中是否有统计数据来监控数据工厂被访问的次数? 那么,举个例子,如果一个自动化系统被设置为通过恶意意图耗尽对 ADF 进行持续的 API 调用
Kafka提供了监控当前偏移量和最新偏移量的能力。同样,azure eventhub是否公开任何api来持续监视分区的当前偏移量和最新可用偏移量? 最佳答案 扩展上述答案,您可以看到两种方式的偏移。
是否有系统 View 或 DMV 记录我的数据仓库恢复和暂停的时间以及执行恢复和暂停的帐户?我环顾四周,似乎找不到具有开/关时间戳的 View 。或者甚至是显示放大和缩小的历史时间戳的 View 。
我一直在研究Microsoft Azure 事件中心。我的目标是找到一种提供自动可扩展性的方法。这是一项实验性工作,我实际上只是想知道我可以使用 Azure 事件中心做什么。我无法访问 Azure 平
我有一个在 azure 中运行的辅助角色。 我正在使用标准跟踪诊断,我可以使用 Visual Studio 中的服务器资源管理器查看该诊断。 但是,它很难涉水,速度很慢等等。 谁能推荐一个插件、工具、
我们将 Azure Function 与 Node.js 结合使用。 在Azure门户UI中,在每个函数调用日志列表旁边(在“监视器”选项卡中),我们看到两个计数器:“最近成功计数”和“最近错误计数”
是否有系统 View 或 DMV 记录我的数据仓库恢复和暂停的时间以及执行恢复和暂停的帐户?我环顾四周,似乎找不到具有开/关时间戳的 View 。或者甚至是显示放大和缩小的历史时间戳的 View 。
我一直在研究Microsoft Azure 事件中心。我的目标是找到一种提供自动可扩展性的方法。这是一项实验性工作,我实际上只是想知道我可以使用 Azure 事件中心做什么。我无法访问 Azure 平
我有一个在 azure 中运行的辅助角色。 我正在使用标准跟踪诊断,我可以使用 Visual Studio 中的服务器资源管理器查看该诊断。 但是,它很难涉水,速度很慢等等。 谁能推荐一个插件、工具、
是否可以获取 channel 消息的副本? (而不是从 channel 接收和删除消息) 这个想法是记录一个 channel 的消息。 最佳答案 Is it possible to get copy
我正在尝试使用 Mockito监视路径 em> dirSpy = spy(Files.createTempDirectory(DIR_NAME)); 我收到一条错误消息 Mockito cannot
我的组件具有以下功能: updateTransactions() { let notes = this.createNotes() let delTransactions = th
我想测试一些在 React 组件的 componentDidMount 生命周期方法中调用的自定义方法。 componentDidMount() { getData().then(res
我的 $scope 中有一个对象,其中包含一些属性,例如: $scope.content = { name : 'myname', description : 'mydescrip
我是一名优秀的程序员,十分优秀!