关于正在讨论监视Collections.sort()进程的可能性的this question,我想知道是否存在一种使用Collections.sort()取消对ProgressMonitor的调用的好方法。

监视进度的一种方法是使用自定义的Comparator来跟踪其调用。

沿着这条线,让这个比较器检查cancelled标志并立即返回0而不进行任何实际比较是否有意义?这样可以节省进行手工比较所需的时间,但不会停止列表元素上的迭代。

public class ProgressMonitoringComparator<T extends Comparable<T>> implements Comparator<T> {

private volatile long invocationCount = 0;
private volatile boolean cancelled = false;
private final long elementCount;
private final ProgressMonitor monitor;

public ProgressMonitoringComparator(long elementCount) {
    this.elementCount = elementCount;
    this.monitor = null;
}

public ProgressMonitoringComparator(ProgressMonitor monitor) {
    this.monitor = monitor;
    this.elementCount = -1;
}

public ProgressMonitoringComparator() {
    this(0);
}

public boolean isCancelled() {

    return cancelled;
}

public void setCancelled(boolean cancelled) {

    this.cancelled = cancelled;
}

public float getProgress() {

    if(elementCount <= 0) {
        return -1;
    }
    long totalInvocationsNeeded = (long)(elementCount * Math.log(elementCount));
    return totalInvocationsNeeded / invocationCount;
}

@Override
public int compare(T o1, T o2) {

    if(cancelled || (monitor != null && monitor.isCancelled())) {
        return 0;
    }
    invocationCount++;
    if(monitor != null) {
        monitor.worked();
    }
    return o1.compareTo(o2);
}


}

如user2717954所建议,第二种方法可能是:

public class CancellableComparator<T extends Comparable<T>> implements Comparator<T> {

    private volatile boolean cancelled = false;

    public boolean isCancelled() {

        return cancelled;
    }

    public void setCancelled(boolean cancelled) {

        this.cancelled = cancelled;
    }

    @Override
    public int compare(T o1, T o2) {

        if(cancelled) {
            throw new CancelException();
        }
        return o1.compareTo(o2);
    }
}

最佳答案

从评论和类似问题中,我得出以下结论:

public class ProgressMonitoringComparator<T extends Comparable<T>> implements Comparator<T> {

    private static final boolean DEFAULT_SOFT = false;
    private static final int DEFAULT_ELEMENT_CNT = -1;
    private volatile long invocationCount = 0;
    private volatile boolean cancelled = false;
    private final long elementCount;
    private final ProgressMonitor monitor;
    private final Comparator<T> delegate;
    private final boolean soft;

    public boolean isCancelled() {

        return cancelled;
    }

    public void setCancelled(boolean cancelled) {

        this.cancelled = cancelled;
    }

    public double getProgress() {

        if(elementCount <= 0) {
            return -1;
        }
        long totalInvocationsNeeded = (long)(elementCount * Math.log(elementCount));
        return totalInvocationsNeeded / (double)invocationCount;
    }

    public ProgressMonitoringComparator(ProgressMonitor monitor, Comparator<T> delegate, boolean soft) {
        super();
        this.elementCount = DEFAULT_ELEMENT_CNT;
        this.monitor = monitor;
        this.delegate = delegate;
        this.soft = soft;
    }

    public ProgressMonitoringComparator(ProgressMonitor monitor, Comparator<T> delegate) {
        this(monitor, delegate, DEFAULT_SOFT);
    }

    public ProgressMonitoringComparator(ProgressMonitor monitor, boolean soft) {
        this(monitor, null, soft);
    }

    public ProgressMonitoringComparator(ProgressMonitor monitor) {
        this(monitor, DEFAULT_SOFT);
    }

    public ProgressMonitoringComparator(long elementCount, Comparator<T> delegate, boolean soft) {
        super();
        this.elementCount = elementCount;
        this.monitor = null;
        this.delegate = delegate;
        this.soft = soft;
    }

    public ProgressMonitoringComparator(long elementCount, boolean soft) {
        this(elementCount, null, soft);
    }

    public ProgressMonitoringComparator(long elementCount) {
        this(elementCount, DEFAULT_SOFT);
    }

    @Override
    public int compare(T o1, T o2) {

        if(cancelled || (monitor != null && monitor.isCancelled())) {
            if(soft) {
                return 0;
            }
            throw new CancelException();
        }
        invocationCount++;
        if(monitor != null) {
            monitor.worked();
        }
        if(delegate != null) {
            return delegate.compare(o1, o2);
        }
        return o1.compareTo(o2);
    }
}

关于java - 如何取消Collections.sort(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43629556/

10-14 12:15