简体   繁体   中英

How to run infinite loop on vertx using while() loop

I would like to run an infinite loop on verx on diff thread. Should be something like this:

vertx.executeBlocking(future -> {
while(true){
}
   //some logic (e.g waiting on blocking-code)
}

thing is that on vertx even for executeBlocking threads you have global timeout which you can increase. but I would like to set for this execution non-timeout warnings as it will run forever

  1. am I achieving my purpose right with vertx?
  2. In case 1 is true. how to exclude this specific blocking execution from the timeout warnings

You don't.

If you have a condition that triggers processing, the have the processing happen at that point and not inside an infinite loop.

Example:

while ((event = eventQueue.take()) != null) {
  final Vertx target = event.target;
  executor.submit(() -> { 
    doProcessing(target);
  });
}

The main reason for this is that even threads in a blocking state do consume resources. If you have 1000 threads waiting on events, then the system is significantly wasting time on thread-scheduling. This is why all modern IO processing happens non-blocking, in a similar fashion as the code above.

I have a similar use case, when I need to consume a blocking api, I have used the following code

@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"));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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