Logo

Programming-Idioms

  • Java

Idiom #230 Timeout

Cancel an ongoing processing p if it has not finished after 5s.

import static java.lang.Thread.sleep;
Thread p = new Thread(() -> {});
p.start();
p.join(5_000);
p.interrupt();

The `join` call will block until `p` exits, or 5 seconds have elapsed.
def main(p) do
  p
  |> Task.async()
  |> Task.await()
end

New implementation...
< >
programming-idioms.org