Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何取消集合。排序()_Java - Fatal编程技术网

Java 如何取消集合。排序()

Java 如何取消集合。排序(),java,Java,关于正在讨论监视Collections.sort()过程的可能性的,我想知道是否有一种好方法可以使用ProgressMonitor取消对Collections.sort()的调用 监视进度的一种方法是使用自定义比较器跟踪其调用 沿着这条线,让这个比较器检查cancelled标志并立即返回0,而不进行任何实际比较,这有意义吗?这将节省进行实际比较所需的时间,但不会停止列表元素的迭代 public class ProgressMonitoringComparator<T extends Com

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

监视进度的一种方法是使用自定义比较器跟踪其调用

沿着这条线,让这个比较器检查
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);
}
公共类ProgressMonitoringComparator实现Comparator{
私有易失性长调用计数=0;
私有易失性布尔值=false;
私人最终长元素计数;
私人最终进展监测;
公共进程监视比较程序(长元素计数){
this.elementCount=elementCount;
this.monitor=null;
}
公共ProgressMonitoringComparator(ProgressMonitor监视器){
this.monitor=监视器;
this.elementCount=-1;
}
公共进度监视比较程序(){
这(0);
}
公共布尔值已取消(){
退货取消;
}
公共作废设置已取消(布尔值已取消){
this.cancelled=已取消;
}
公共进度(){

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

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);
    }
}
公共类ProgressMonitoringComparator实现Comparator{
私有静态最终布尔默认值_SOFT=false;
私有静态final int DEFAULT_ELEMENT_CNT=-1;
私有易失性长调用计数=0;
私有易失性布尔值=false;
私人最终长元素计数;
私人最终进展监测;
非公开最终比较国代表;
私有最终布尔软;
公共布尔值已取消(){
退货取消;
}
公共作废设置已取消(布尔值已取消){
this.cancelled=已取消;
}
公共双getProgress(){

if(elementCount Collections.sort()不会向您提供有关其进度的任何信息。您可以杀死正在运行的线程,但这并不是一个好方法。@f1sh这是我的第一个想法,但是……可以吗?Java依赖于符合中断的线程,不可以吗?您无法强制它完成。而且,如果您碰巧在交换或其他过程中停止了它,您将被留下数据结构可能不完整。@Michael您可以只调用
Thread.stop()
,但不应该调用。当然,您的数据将处于未定义、未完成的状态。当您取消在传递给它的数据结构上运行的操作时,您还希望看到什么?@f1sh如果
Collections.sort()
是通过偶尔检查中断来实现的,那么您可能会没事。我99%确定不会。在比较时是否抛出异常?