ActorThread updates
This commit is contained in:
parent
71db7d9e8c
commit
2eeb21c5ce
1 changed files with 13 additions and 27 deletions
|
@ -2,7 +2,6 @@ package actor;
|
||||||
|
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public abstract class ActorThread<M> extends Thread {
|
public abstract class ActorThread<M> extends Thread {
|
||||||
|
|
||||||
|
@ -10,42 +9,29 @@ public abstract class ActorThread<M> extends Thread {
|
||||||
|
|
||||||
/** Called by another thread, to send a message to this thread. */
|
/** Called by another thread, to send a message to this thread. */
|
||||||
public void send(M message) {
|
public void send(M message) {
|
||||||
System.out.println(" # Sending message: " + message);
|
String handlerName = this.getClass().getSimpleName();
|
||||||
q.offer(message);
|
q.offer(message);
|
||||||
System.out.println(" % Queue State: " + q);
|
System.out.println(" # Message to " + handlerName + " : " + message);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the first message in the queue, or blocks if none available. */
|
/** Returns the first message in the queue, or blocks if none available. */
|
||||||
protected M receive() throws InterruptedException {
|
protected M receive() throws InterruptedException {
|
||||||
M mess = q.take();
|
M mess = q.take();
|
||||||
System.out.println(" - Message taken: " + mess);
|
|
||||||
return mess;
|
return mess;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the first message in the queue, or blocks up to 'timeout'
|
/**
|
||||||
milliseconds if none available. Returns null if no message is obtained
|
* Returns the first message in the queue, or blocks up to 'timeout'
|
||||||
within 'timeout' milliseconds. */
|
* milliseconds if none available. Returns null if no message is obtained
|
||||||
|
* within 'timeout' milliseconds.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
protected M receiveWithTimeout(long timeout) throws InterruptedException {
|
protected M receiveWithTimeout(long timeout) throws InterruptedException {
|
||||||
return q.poll(timeout, TimeUnit.MILLISECONDS);
|
return q.poll(timeout, TimeUnit.MILLISECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Optional<M> poll(long timeOut) {
|
/** Wait for a message continuously */
|
||||||
try {
|
protected M take() throws InterruptedException {
|
||||||
Optional<M> m = Optional.ofNullable(q.poll(timeOut, TimeUnit.MILLISECONDS));
|
return q.take();
|
||||||
System.out.println("Stolen: " + m);
|
|
||||||
return m;
|
|
||||||
} 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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue