如何取消Collections.sort()

时间:2017-04-26 09:01:12

标签: java

关于this question,正在讨论监控Collections.sort()过程的可能性,我想知道是否有一种好方法可以取消对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);
}

}

用户2717954建议的第二种方法可能如下:

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);
    }
}

1 个答案:

答案 0 :(得分:2)

从评论和类似问题中,我想出了以下内容:

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);
    }
}