如何使用 while() 循环在顶点上 运行 无限循环
How to run infinite loop on vertx using while() loop
我想 运行 在 diff 线程上的 verx 上进行无限循环。
应该是这样的:
vertx.executeBlocking(future -> {
while(true){
}
//some logic (e.g waiting on blocking-code)
}
事情是,在 vertx 上,即使对于 executeBlocking 线程,你也有全局超时,你可以增加它。但我想为此执行设置非超时警告,因为它将 运行 永远
- 我是否通过 vertx 实现了我的目的?
- 假设情况1成立。如何从超时警告中排除这个特定的阻塞执行
你不知道。
如果您有触发处理的条件,则处理会在该点发生,而不是在无限循环内。
示例:
while ((event = eventQueue.take()) != null) {
final Vertx target = event.target;
executor.submit(() -> {
doProcessing(target);
});
}
主要原因是即使线程处于阻塞状态也会消耗资源。如果您有 1000 个线程在等待事件,那么系统会在线程调度上浪费大量时间。这就是为什么所有现代 IO 处理都以非阻塞方式发生,其方式与上面的代码类似。
我有一个类似的用例,当我需要消耗一个阻塞时api,我使用了下面的代码
@Override
public void start() {
vertx.<Void>executeBlocking(f -> {
while (true) {
// blocking...
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
vertx.eventBus().publish("channel.1", "msg");
}
}, voidAsyncResult -> System.out.println("done"));
}
我想 运行 在 diff 线程上的 verx 上进行无限循环。 应该是这样的:
vertx.executeBlocking(future -> {
while(true){
}
//some logic (e.g waiting on blocking-code)
}
事情是,在 vertx 上,即使对于 executeBlocking 线程,你也有全局超时,你可以增加它。但我想为此执行设置非超时警告,因为它将 运行 永远
- 我是否通过 vertx 实现了我的目的?
- 假设情况1成立。如何从超时警告中排除这个特定的阻塞执行
你不知道。
如果您有触发处理的条件,则处理会在该点发生,而不是在无限循环内。
示例:
while ((event = eventQueue.take()) != null) {
final Vertx target = event.target;
executor.submit(() -> {
doProcessing(target);
});
}
主要原因是即使线程处于阻塞状态也会消耗资源。如果您有 1000 个线程在等待事件,那么系统会在线程调度上浪费大量时间。这就是为什么所有现代 IO 处理都以非阻塞方式发生,其方式与上面的代码类似。
我有一个类似的用例,当我需要消耗一个阻塞时api,我使用了下面的代码
@Override
public void start() {
vertx.<Void>executeBlocking(f -> {
while (true) {
// blocking...
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
vertx.eventBus().publish("channel.1", "msg");
}
}, voidAsyncResult -> System.out.println("done"));
}