Package org.apache.mina.core.future
Class DefaultIoFuture
- java.lang.Object
-
- org.apache.mina.core.future.DefaultIoFuture
-
- All Implemented Interfaces:
IoFuture
- Direct Known Subclasses:
AbstractIoService.ServiceOperationFuture,CompositeIoFuture,DefaultCloseFuture,DefaultConnectFuture,DefaultReadFuture,DefaultWriteFuture
public class DefaultIoFuture extends java.lang.Object implements IoFuture
-
-
Field Summary
Fields Modifier and Type Field Description private static longDEAD_LOCK_CHECK_INTERVALA number of milliseconds to wait between two deadlock controls ( 5 seconds )private IoFutureListener<?>firstListenerThe first listener.private java.lang.ObjectlockA lock used by the wait() methodprivate java.util.List<IoFutureListener<?>>otherListenersAll the other listeners, in case we have more than oneprivate booleanreadyThe flag used to determinate if the Future is completed or notprivate java.lang.Objectresultprivate IoSessionsessionThe associated sessionprivate intwaitersA counter for the number of threads waiting on this future
-
Constructor Summary
Constructors Constructor Description DefaultIoFuture(IoSession session)Creates a new instance associated with anIoSession.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description IoFutureaddListener(IoFutureListener<?> listener)Adds an eventlistenerwhich is notified when this future is completed.IoFutureawait()Wait for the asynchronous operation to complete.booleanawait(long timeoutMillis)Wait for the asynchronous operation to complete with the specified timeout.booleanawait(long timeout, java.util.concurrent.TimeUnit unit)Wait for the asynchronous operation to complete with the specified timeout.private booleanawait0(long timeoutMillis, boolean interruptable)Wait for the Future to be ready.IoFutureawaitUninterruptibly()Wait for the asynchronous operation to complete uninterruptibly.booleanawaitUninterruptibly(long timeoutMillis)Wait for the asynchronous operation to complete with the specified timeout uninterruptibly.booleanawaitUninterruptibly(long timeout, java.util.concurrent.TimeUnit unit)Wait for the asynchronous operation to complete with the specified timeout uninterruptibly.private voidcheckDeadLock()Check for a deadlock, ie look into the stack trace that we don't have already an instance of the caller.IoSessiongetSession()protected java.lang.ObjectgetValue()booleanisDone()voidjoin()Deprecated.Replaced withawaitUninterruptibly().booleanjoin(long timeoutMillis)Deprecated.Replaced withawaitUninterruptibly(long).private voidnotifyListener(IoFutureListener listener)private voidnotifyListeners()Notify the listeners, if we have some.IoFutureremoveListener(IoFutureListener<?> listener)Removes an existing eventlistenerso it won't be notified when the future is completed.booleansetValue(java.lang.Object newValue)Sets the result of the asynchronous operation, and mark it as finished.
-
-
-
Field Detail
-
DEAD_LOCK_CHECK_INTERVAL
private static final long DEAD_LOCK_CHECK_INTERVAL
A number of milliseconds to wait between two deadlock controls ( 5 seconds )- See Also:
- Constant Field Values
-
session
private final IoSession session
The associated session
-
lock
private final java.lang.Object lock
A lock used by the wait() method
-
firstListener
private IoFutureListener<?> firstListener
The first listener. This is easier to have this variable when we most of the time have one single listener
-
otherListeners
private java.util.List<IoFutureListener<?>> otherListeners
All the other listeners, in case we have more than one
-
result
private java.lang.Object result
-
ready
private boolean ready
The flag used to determinate if the Future is completed or not
-
waiters
private int waiters
A counter for the number of threads waiting on this future
-
-
Method Detail
-
getSession
public IoSession getSession()
- Specified by:
getSessionin interfaceIoFuture- Returns:
- the
IoSessionwhich is associated with this future.
-
join
@Deprecated public void join()
Deprecated.Replaced withawaitUninterruptibly().
-
join
@Deprecated public boolean join(long timeoutMillis)
Deprecated.Replaced withawaitUninterruptibly(long).
-
await
public IoFuture await() throws java.lang.InterruptedException
Wait for the asynchronous operation to complete. The attached listeners will be notified when the operation is completed.
-
await
public boolean await(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedExceptionWait for the asynchronous operation to complete with the specified timeout.
-
await
public boolean await(long timeoutMillis) throws java.lang.InterruptedExceptionWait for the asynchronous operation to complete with the specified timeout.
-
awaitUninterruptibly
public IoFuture awaitUninterruptibly()
Wait for the asynchronous operation to complete uninterruptibly. The attached listeners will be notified when the operation is completed.- Specified by:
awaitUninterruptiblyin interfaceIoFuture- Returns:
- the current IoFuture
-
awaitUninterruptibly
public boolean awaitUninterruptibly(long timeout, java.util.concurrent.TimeUnit unit)Wait for the asynchronous operation to complete with the specified timeout uninterruptibly.- Specified by:
awaitUninterruptiblyin interfaceIoFuture- Parameters:
timeout- The maximum delay to wait before getting outunit- the type of unit for the delay (seconds, minutes...)- Returns:
trueif the operation is completed.
-
awaitUninterruptibly
public boolean awaitUninterruptibly(long timeoutMillis)
Wait for the asynchronous operation to complete with the specified timeout uninterruptibly.- Specified by:
awaitUninterruptiblyin interfaceIoFuture- Parameters:
timeoutMillis- The maximum milliseconds to wait before getting out- Returns:
trueif the operation is finished.
-
await0
private boolean await0(long timeoutMillis, boolean interruptable) throws java.lang.InterruptedExceptionWait for the Future to be ready. If the requested delay is 0 or negative, this method immediately returns the value of the 'ready' flag. Every 5 second, the wait will be suspended to be able to check if there is a deadlock or not.- Parameters:
timeoutMillis- The delay we will wait for the Future to be readyinterruptable- Tells if the wait can be interrupted or not- Returns:
trueif the Future is ready- Throws:
java.lang.InterruptedException- If the thread has been interrupted when it's not allowed.
-
checkDeadLock
private void checkDeadLock()
Check for a deadlock, ie look into the stack trace that we don't have already an instance of the caller.
-
isDone
public boolean isDone()
-
setValue
public boolean setValue(java.lang.Object newValue)
Sets the result of the asynchronous operation, and mark it as finished.- Parameters:
newValue- The result to store into the Future- Returns:
trueif the value has been set,falseif the future already has a value (thus is in ready state)
-
getValue
protected java.lang.Object getValue()
- Returns:
- the result of the asynchronous operation.
-
addListener
public IoFuture addListener(IoFutureListener<?> listener)
Adds an eventlistenerwhich is notified when this future is completed. If the listener is added after the completion, the listener is directly notified.- Specified by:
addListenerin interfaceIoFuture- Parameters:
listener- The listener to add- Returns:
- the current IoFuture
-
removeListener
public IoFuture removeListener(IoFutureListener<?> listener)
Removes an existing eventlistenerso it won't be notified when the future is completed.- Specified by:
removeListenerin interfaceIoFuture- Parameters:
listener- The listener to remove- Returns:
- the current IoFuture
-
notifyListeners
private void notifyListeners()
Notify the listeners, if we have some.
-
notifyListener
private void notifyListener(IoFutureListener listener)
-
-