1 SelectStrategy interface

Provides the ability to control the behavior of select loops.Copy the code

1.1 Method Signature:

int calculateStrategy(IntSupplier selectSupplier, boolean hasTasks) throws Exception;
Copy the code

Used after a SELECT call to determine how to process the results of the select;

1.2 the reference:

The selectSupplier interface returns an int from the select call;

Is there a task waiting to be performed?

1.3 the return value

int SELECT = -1; Indicates that blocking SELECT should be invoked nextCopy the code
int CONTINUE = -2; Indicates that the IO loop should be retried in the next stepCopy the code
Copy the code
int BUSY_WAIT = -3; Indicates that non-blocking SELECT should be invoked next;Copy the code

If the return value is greater than or equal to 0, several ready channels should now be processed;

The default implementation of 2 SelectStrategy is DefaultSelectStrategy

In Netty, SelectStrategy is an interface. You can customize your implementation and set up your implementation to make Netty adopt your implementation, but if you don’t need to customize, Netty will use the default implementation it provides: DefaultSelectStrategy.

final class DefaultSelectStrategy implements SelectStrategy { static final SelectStrategy INSTANCE = new DefaultSelectStrategy(); private DefaultSelectStrategy() { } @Override public int calculateStrategy(IntSupplier selectSupplier, boolean hasTasks) throws Exception { return hasTasks ? selectSupplier.get() : SelectStrategy.SELECT; }}Copy the code

As seen above, static directly initializes an instance of DefaultSelectStrategy;

Selectstrategy. SELECT is returned if there is no task (hasTasks=false), and the value of SELECT is returned if there is a task.

3 SelectStrategyFactory

Is an interface for producing selectStrategies;

Code:

public interface SelectStrategyFactory {
 
    /**
     * Creates a new {@link SelectStrategy}.
     */
    SelectStrategy newSelectStrategy();
}
Copy the code

4 DefaultSelectStrategyFactory

The default implementation of SelectStrategyFactory returns the same instance of DefaultSelectStrategy each time:

public final class DefaultSelectStrategyFactory implements SelectStrategyFactory { public static final SelectStrategyFactory INSTANCE = new DefaultSelectStrategyFactory(); private DefaultSelectStrategyFactory() { } @Override public SelectStrategy newSelectStrategy() { return DefaultSelectStrategy.INSTANCE; }}Copy the code