Netty's strategy in those modes

Features strategists mode

When inheritance system design class, we will deliberate the common parts are extracted to the base class

For example, to design the Person class, the humans had been placed in the Person of behavior, peculiar behavior is designed to abstract way for concrete subclasses to achieve, so follow-up whether we go to construct student, teacher or construction, we all inherit Person, to achieve the purpose of code reuse

But this question came up, the class teacher, the teaching of the need for behavior, if this method is in the form of an abstract base class method, then inherited the students for class Person is not right, because there is no requirement of students We will be teaching, but students now have to implement this method

If we take the behavior of the teacher's teaching the class as a private teacher, this time, Li Ming Jiao little learning, it means that Xiao Ming, he needs teaching behavior, conflicts began to look back and forth, and in the end how to deal with it?

Strategists mode, to solve this problem, it has become an act of abstract interfaces to implement the interface and the way to solve the above problem, the above example, the teaching can be designed to interface to any class, as long as the realization of the this interface can be teaching, but not necessarily mandatory teacher can only achieve it

Overall, the strategy mode, the behavior is abstracted into + mode interfaces implemented

Use Netty strategists mode

netty the bossgroupreceived new use after connecting the selector Chooser, from WorkerGroupthe selected one EventLoop, and this is connected into the selected registerEventLoop

netty selector is strategists mode, the selection of behavior designed to use interface to select different options implemented without interface with different ways according to their needs

Behavior Interface

@UnstableApi
public interface EventExecutorChooserFactory {

EventExecutorChooser newChooser(EventExecutor[] executors);
@UnstableApi
interface EventExecutorChooser {
    EventExecutor next();
}
}

Different implementations selector:

if (isPowerOfTwo(executors.length)) {// todo 如果是2的指数倍, 返回PowerOfTwoEventExecutorChooser
    return new PowerOfTwoEventExecutorChooser(executors);
} else {// todo  否则返回同样的实例
    return new GenericEventExecutorChooser(executors);
}

Depending on the number of threads actuator determine the kind of specific behavior

Behavior 1:PowerOfTwoEventExecutorChooser

private static final class PowerOfTwoEventExecutorChooser implements EventExecutorChooser {
    private final AtomicInteger idx = new AtomicInteger();
    private final EventExecutor[] executors;
    PowerOfTwoEventExecutorChooser(EventExecutor[] executors) {
        this.executors = executors;
    }

    @Override
    public EventExecutor next() {
        return executors[idx.getAndIncrement() & executors.length - 1];
    }

Mainly to see itsexecutors[idx.getAndIncrement() & executors.length - 1]

And the operation carried out faster

1 & 1 = 1
1 & 0 = 0
0 & 1 = 0

When the length of the array is a power of 2, the binary notation is 1111 ... are all 1, minus 1, it is 0111 ...

No matter who the previous number is that for a 0111 ... with the operation, the result is the number 0-0111 ... from the size of the cycle

Behavior 2:GenericEventExecutorChooser

private final AtomicInteger idx = new AtomicInteger();
private final EventExecutor[] executors;

GenericEventExecutorChooser(EventExecutor[] executors) {
    this.executors = executors;
}

@Override
public EventExecutor next() {
    // todo 从0开始到最后一个, 再从零开始,到最后一个
    return executors[Math.abs(idx.getAndIncrement() % executors.length)];
}

The main step is to Math.abs(idx.getAndIncrement() % executors.length)
be seen, starting from 0 up later take the remainder of the length of the array, decimal to large numbers take the remainder = decimal ensure the array index starts from 0 increments that he take the remainder of their = 0, guarantee It is the maximum length of the array minus one, and so forth

Guess you like

Origin www.cnblogs.com/ZhuChangwu/p/11237899.html