ActorThread
This commit is contained in:
parent
820687f59b
commit
9cf56e98c5
1 changed files with 8 additions and 7 deletions
|
@ -1,25 +1,26 @@
|
||||||
package actor;
|
package actor;
|
||||||
|
|
||||||
public abstract class ActorThread<M> extends Thread {
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
// TODO: one suitable attribute here
|
public class ActorThread<M> extends Thread {
|
||||||
|
|
||||||
|
private final LinkedBlockingQueue<M> q = new LinkedBlockingQueue<>();
|
||||||
|
|
||||||
/** 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) {
|
||||||
// TODO: implement this method (one or a few lines)
|
q.offer(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 {
|
||||||
// TODO: implement this method (one or a few lines)
|
return q.take();
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the first message in the queue, or blocks up to 'timeout'
|
/** Returns the first message in the queue, or blocks up to 'timeout'
|
||||||
milliseconds if none available. Returns null if no message is obtained
|
milliseconds if none available. Returns null if no message is obtained
|
||||||
within 'timeout' milliseconds. */
|
within 'timeout' milliseconds. */
|
||||||
protected M receiveWithTimeout(long timeout) throws InterruptedException {
|
protected M receiveWithTimeout(long timeout) throws InterruptedException {
|
||||||
// TODO: implement this method (one or a few lines)
|
return q.poll(timeout, TimeUnit.MILLISECONDS);
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue