From 1f859d1a2870108cb54ade3cf2141cd3a8b2a732 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Sun, 3 Nov 2024 16:18:59 +0100 Subject: [PATCH] ActorThread extended with timeout poll with Optional return type --- wash/src/actor/ActorThread.java | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/wash/src/actor/ActorThread.java b/wash/src/actor/ActorThread.java index b7f992f..558aa21 100644 --- a/wash/src/actor/ActorThread.java +++ b/wash/src/actor/ActorThread.java @@ -4,7 +4,7 @@ import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; import java.util.Optional; -public class ActorThread extends Thread { +public abstract class ActorThread extends Thread { private final LinkedBlockingQueue q = new LinkedBlockingQueue<>(); @@ -25,7 +25,21 @@ public class ActorThread extends Thread { return q.poll(timeout, TimeUnit.MILLISECONDS); } - protected Optional poll(long timeOut) throws InterruptedException { - return Optional.ofNullable(q.poll(timeOut, TimeUnit.MILLISECONDS)); + protected Optional poll(long timeOut) { + try { + return Optional.ofNullable(q.poll(timeOut, TimeUnit.MILLISECONDS)); + } catch (InterruptedException e) { + return Optional.empty(); + } catch (Exception e) { + return Optional.empty(); // Or exit the program + } + } + + protected void usleep(long time) { + try { + Thread.sleep(time); + } catch (InterruptedException e) { + e.printStackTrace(); + } } } \ No newline at end of file