背景
最近在接手跑路同事的项目,遇到一个Ordered 的interface
代码如下
public interface Handler<Msg extends HandlerMsgDTO> extends Ordered {
default int getOrder() {
return Integer.MAX_VALUE;
}
List<String> getHandleMsgType();
default void handle(List<Msg> msgList) throws Exception {
throw new UnsupportedOperationException();
}
default void handle(Msg msg) throws Exception {
throw new UnsupportedOperationException();
}
}
所以想了解Ordered 是越大越先执行还是越小越先执行
翻看注释
Ordered is an interface that can be implemented by objects that should be orderable, for example in a Collection.
The actual order can be interpreted as prioritization, with the first object (with the lowest order value) having the highest priority.
Note that there is also a priority marker for this interface: PriorityOrdered. Consult the Javadoc for PriorityOrdered for details on how PriorityOrdered objects are ordered relative to plain Ordered objects.
Consult the Javadoc for OrderComparator for details on the sort semantics for non-ordered objects.
核心是这句:
The actual order can be interpreted as prioritization, with the first object (with the lowest order value) having the highest priority.
最小的order值有最大的优先级
总结
按照order值从小到大执行 ,所以最先执行的是Interger.MIN_VALUE , 最后执行的是Interger.MAX_VALUE