Example usage for org.eclipse.jdt.internal.core.search.processing IJob cancel

List of usage examples for org.eclipse.jdt.internal.core.search.processing IJob cancel

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core.search.processing IJob cancel.

Prototype

public void cancel();

Source Link

Document

Asks this job to cancel its execution.

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.search.processing.JobManager.java

License:Open Source License

/**
 * Remove the index from cache for a given project.
 * Passing null as a job family discards them all.
 *//* w  ww  . ja  v a 2s  .  c  om*/
public void discardJobs(String jobFamily) {

    if (VERBOSE)
        Util.verbose("DISCARD   background job family - " + jobFamily); //$NON-NLS-1$

    try {
        org.eclipse.jdt.internal.core.search.processing.IJob currentJob;
        // cancel current job if it belongs to the given family
        synchronized (this) {
            currentJob = currentJob();
            disable();
        }
        if (currentJob != null && (jobFamily == null || currentJob.belongsTo(jobFamily))) {
            currentJob.cancel();

            // wait until current active job has finished
            while (this.processingThread != null && this.executing) {
                try {
                    if (VERBOSE)
                        Util.verbose("-> waiting end of current background job - " + currentJob); //$NON-NLS-1$
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    // ignore
                }
            }
        }

        // flush and compact awaiting jobs
        int loc = -1;
        synchronized (this) {
            for (int i = this.jobStart; i <= this.jobEnd; i++) {
                currentJob = this.awaitingJobs[i];
                if (currentJob != null) { // sanity check
                    this.awaitingJobs[i] = null;
                    if (!(jobFamily == null || currentJob.belongsTo(jobFamily))) { // copy down, compacting
                        this.awaitingJobs[++loc] = currentJob;
                    } else {
                        if (VERBOSE)
                            Util.verbose("-> discarding background job  - " + currentJob); //$NON-NLS-1$
                        currentJob.cancel();
                    }
                }
            }
            this.jobStart = 0;
            this.jobEnd = loc;
        }
    } finally {
        enable();
    }
    if (VERBOSE)
        Util.verbose("DISCARD   DONE with background job family - " + jobFamily); //$NON-NLS-1$
}