Skip to content
New issue

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

ActionsImpl getSortedAction 方法会触发并发问题 #145

Open
Angry-Rabbit opened this issue Nov 23, 2023 · 1 comment
Open

ActionsImpl getSortedAction 方法会触发并发问题 #145

Angry-Rabbit opened this issue Nov 23, 2023 · 1 comment

Comments

@Angry-Rabbit
Copy link

Angry-Rabbit commented Nov 23, 2023

多线程首次执行状态机时,会出现并发问题

原来代码:

`

@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;
}`
@huan670119408
Copy link

正解,初次执行时,并发情况下 可能会抛ConcurrentModificationException异常

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants