We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
多线程首次执行状态机时,会出现并发问题
原来代码:
`
@Override public List<Action<T, S, E, C>> getAll() { return getSortedAction(); } private List<Action<T, S, E, C>> getSortedAction() { if(actions.isEmpty()) return Collections.emptyList(); if(sortedActions==null) { //问题1 这里多个线程同时能进来, sortedActions = Lists.newArrayList(actions); // 问题2 提前的赋值会造成其它线程提早拿到一个未排序的actions Collections.sort(sortedActions, new Comparator<Action<T, S, E, C>>() { // 问题3 Collections.sort会影响sortedActions这个共有集合的可操作性,进而引发异常 @Override public int compare(Action<T, S, E, C> o1, Action<T, S, E, C> o2) { return o2.weight() - o1.weight(); } }); sortedActions = Collections.unmodifiableList(sortedActions); } return sortedActions; }`
因我idea连接不上github所以, 解决的代码粘在下面给作者参考:
@Override public List<Action<T, S, E, C>> getAll() { return sortedActions != null ? sortedActions : getSortedAction(); } private synchronized List<Action<T, S, E, C>> getSortedAction() { if (sortedActions != null) { return sortedActions; } if (!actions.isEmpty()) { List<Action<T, S, E, C>> tmpActions = Lists.newArrayList(this.actions); Collections.sort(tmpActions, (o1, o2) -> o2.weight() - o1.weight()); sortedActions = Collections.unmodifiableList(tmpActions); } else { sortedActions = Collections.emptyList(); } return sortedActions; }`
The text was updated successfully, but these errors were encountered:
正解,初次执行时,并发情况下 可能会抛ConcurrentModificationException异常
Sorry, something went wrong.
No branches or pull requests
多线程首次执行状态机时,会出现并发问题
原来代码:
`
因我idea连接不上github所以, 解决的代码粘在下面给作者参考:
`
The text was updated successfully, but these errors were encountered: