Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2000, 2011 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: IBM Corporation - initial API and implementation /
 
 package org.eclipse.jdt.internal.core.search.processing;
 
 import  org.eclipse.core.runtime.*;
 import  org.eclipse.core.runtime.jobs.Job;
 
 public abstract class JobManager implements Runnable {
 
 	/* queue of jobs to execute */
 	protected IJob[] awaitingJobs = new IJob[10];
 	protected int jobStart = 0;
 	protected int jobEnd = -1;
 	protected boolean executing = false;
 
 	/* background processing */
 	protected Thread processingThread;
 	protected Job progressJob;
 
 	/* counter indicating whether job execution is enabled or not, disabled if <= 0
 	    it cannot go beyond 1 */
 	private int enableCount = 1;
 
 	public static boolean VERBOSE = false;
 	/* flag indicating that the activation has completed */
 	public boolean activated = false;
 
 	private int awaitingClients = 0;

Invoked exactly once, in background, before starting processing any job
 
 	public void activateProcessing() {
 		this. = true;
 	}
Answer the amount of awaiting jobs.
 
 	public synchronized int awaitingJobsCount() {
 		// pretend busy in case concurrent job attempts performing before activated
 		return this. ? this. - this. + 1 : 1;
 	}
Answers the first job in the queue, or null if there is no job available Until the job has completed, the job manager will keep answering the same job.
 
 	public synchronized IJob currentJob() {
 		if (this. > 0 && this. <= this.)
 			return this.[this.];
 		return null;
 	}
 	public synchronized void disable() {
 		this.--;
 		if ()
 			Util.verbose("DISABLING background indexing"); //$NON-NLS-1$
 	}
Remove the index from cache for a given project. Passing null as a job family discards them all.
 
 	public void discardJobs(String jobFamily) {
 
 		if ()
 			Util.verbose("DISCARD   background job family - " + jobFamily); //$NON-NLS-1$
 
 		try {
 			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. != null && this.){
 					try {
 						if ()
 							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.i <= this.i++) {
					currentJob = this.[i];
					if (currentJob != null) { // sanity check
						this.[i] = null;
						if (!(jobFamily == null || currentJob.belongsTo(jobFamily))) { // copy down, compacting
							this.[++loc] = currentJob;
else {
							if ()
								Util.verbose("-> discarding background job  - " + currentJob); //$NON-NLS-1$
							currentJob.cancel();
						}
					}
				}
				this. = 0;
				this. = loc;
			}
finally {
		}
		if ()
			Util.verbose("DISCARD   DONE with background job family - " + jobFamily); //$NON-NLS-1$
	}
	public synchronized void enable() {
		if ()
			Util.verbose("ENABLING  background indexing"); //$NON-NLS-1$
		notifyAll(); // wake up the background thread if it is waiting (context must be synchronized)
	}
	protected synchronized boolean isJobWaiting(IJob request) {
		for (int i = this.i > this.i--) // don't check job at jobStart, as it may have already started
			if (request.equals(this.[i])) return true;
		return false;
	}
Advance to the next available job, once the current one has been completed. Note: clients awaiting until the job count is zero are still waiting at this point.
	protected synchronized void moveToNextJob() {
		//if (!enabled) return;
		if (this. <= this.) {
			this.[this.++] = null;
			if (this. > this.) {
				this. = 0;
				this. = -1;
			}
		}
	}
When idle, give chance to do something
	protected void notifyIdle(long idlingTime) {
		// do nothing
	}
This API is allowing to run one job in concurrence with background processing. Indeed since other jobs are performed in background, resource sharing might be an issue.Therefore, this functionality allows a given job to be run without colliding with background ones. Note: multiple thread might attempt to perform concurrent jobs at the same time, and should synchronize (it is deliberately left to clients to decide whether concurrent jobs might interfere or not. In general, multiple read jobs are ok). Waiting policy can be: IJobConstants.ForceImmediateSearch IJobConstants.CancelIfNotReadyToSearch IJobConstants.WaitUntilReadyToSearch
	public boolean performConcurrentJob(IJob searchJobint waitingPolicy, IProgressMonitor progress) {
		if ()
			Util.verbose("STARTING  concurrent job - " + searchJob); //$NON-NLS-1$
		searchJob.ensureReadyToRun();
		boolean status = .;
		try {
			int concurrentJobWork = 100;
			if (progress != null)
				progress.beginTask(""concurrentJobWork); //$NON-NLS-1$
			if (awaitingJobsCount() > 0) {
				switch (waitingPolicy) {
						if ()
							Util.verbose("-> NOT READY - forcing immediate - " + searchJob);//$NON-NLS-1$
						try {
							disable(); // pause indexing
							status = searchJob.execute(progress == null ? null : new SubProgressMonitor(progressconcurrentJobWork));
finally {
						}
						if ()
							Util.verbose("FINISHED  concurrent job - " + searchJob); //$NON-NLS-1$
						return status;
						if ()
							Util.verbose("-> NOT READY - cancelling - " + searchJob); //$NON-NLS-1$
						if ()
							Util.verbose("CANCELED concurrent job - " + searchJob); //$NON-NLS-1$
						throw new OperationCanceledException();
						IProgressMonitor subProgress = null;
						try {
							int totalWork = 1000;
							if (progress != null) {
								subProgress = new SubProgressMonitor(progressconcurrentJobWork * 8 / 10);
								subProgress.beginTask(""totalWork); //$NON-NLS-1$
								concurrentJobWork = concurrentJobWork * 2 / 10;
							}
							// use local variable to avoid potential NPE (see bug 20435 NPE when searching java method
							// and bug 42760 NullPointerException in JobManager when searching)
							int originalPriority = t == null ? -1 : t.getPriority();
							try {
								if (t != null)
								synchronized(this) {
								}
								IJob previousJob = null;
								int awaitingJobsCount;
								int lastJobsCount = totalWork;
								float lastWorked = 0;
								float totalWorked = 0;
								while ((awaitingJobsCount = awaitingJobsCount()) > 0) {
									if ((subProgress != null && subProgress.isCanceled())
											|| this. == null)
										throw new OperationCanceledException();
									IJob currentJob = currentJob();
									// currentJob can be null when jobs have been added to the queue but job manager is not enabled
									if (currentJob != null && currentJob != previousJob) {
										if ()
											Util.verbose("-> NOT READY - waiting until ready - " + searchJob);//$NON-NLS-1$
										if (subProgress != null) {
											String indexing = Messages.bind(.currentJob.getJobFamily(), Integer.toString(awaitingJobsCount));
											subProgress.subTask(indexing);
											// ratio of the amount of work relative to the total work
											float ratio = awaitingJobsCount < totalWork ? 1 : ((floattotalWork) / awaitingJobsCount;
											if (lastJobsCount > awaitingJobsCount) {
												totalWorked += (lastJobsCount - awaitingJobsCount) * ratio;
else {
												// more jobs were added, just increment by the ratio
												totalWorked += ratio;
											}
											if (totalWorked - lastWorked >= 1) {
												subProgress.worked((int) (totalWorked - lastWorked));
												lastWorked = totalWorked;
											}
											lastJobsCount = awaitingJobsCount;
										}
										previousJob = currentJob;
									}
									try {
										if ()
											Util.verbose("-> GOING TO SLEEP - " + searchJob);//$NON-NLS-1$
										Thread.sleep(50);
catch (InterruptedException e) {
										// ignore
									}
								}
finally {
								synchronized(this) {
								}
								if (t != null && originalPriority > -1 && t.isAlive())
									t.setPriority(originalPriority);
							}
finally {
							if (subProgress != null)
								subProgress.done();
						}
				}
			}
			status = searchJob.execute(progress == null ? null : new SubProgressMonitor(progressconcurrentJobWork));
finally {
			if (progress != null)
				progress.done();
			if ()
				Util.verbose("FINISHED  concurrent job - " + searchJob); //$NON-NLS-1$
		}
		return status;
	}
	public abstract String processName();
	public synchronized void request(IJob job) {
		// append the job to the list of ones to process later on
		int size = this..length;
		if (++this. == size) { // when growing, relocate jobs starting at position 0
			this. -= this.// jobEnd now equals the number of jobs
			if (this. < 50 && this. < this.) {
				// plenty of free space in the queue so shift the remaining jobs to the beginning instead of growing it
				System.arraycopy(this.this.this., 0, this.);
				for (int i = this.i < sizei++)
					this.[i] = null;
else {
				System.arraycopy(this.this.this. = new IJob[size * 2], 0, this.);
			}
			this. = 0;
		}
		this.[this.] = job;
		if () {
			Util.verbose("REQUEST   background job - " + job); //$NON-NLS-1$
			Util.verbose("AWAITING JOBS count: " + awaitingJobsCount()); //$NON-NLS-1$
		}
		notifyAll(); // wake up the background thread if it is waiting
	}
Flush current state
	public synchronized void reset() {
		if ()
			Util.verbose("Reset"); //$NON-NLS-1$
		if (this. != null) {
			discardJobs(null); // discard all jobs
else {
			/* initiate background processing */
			this. = new Thread(thisprocessName());
			// less prioritary by default, priority is raised if clients are actively waiting on it
			// https://bugs.eclipse.org/bugs/show_bug.cgi?id=296343
			// set the context loader to avoid leaking the current context loader
		}
	}
Infinite loop performing resource indexing
	public void run() {
		long idlingStart = -1;
		try {
			class ProgressJob extends Job {
					super(name);
				}
				protected IStatus run(IProgressMonitor monitor) {
					IJob job = currentJob();
					while (!monitor.isCanceled() && job != null) {
						monitor.subTask(taskName);
						setName(taskName);
						try {
							Thread.sleep(500);
catch (InterruptedException e) {
							// ignore
						}
						job = currentJob();
					}
					return Status.OK_STATUS;
				}
			}
			this. = null;
			while (this. != null) {
				try {
					IJob job;
					synchronized (this) {
						// handle shutdown case when notifyAll came before the wait but after the while loop was entered
						if (this. == nullcontinue;
						// must check for new job inside this sync block to avoid timing hole
						if ((job = currentJob()) == null) {
							if (this. != null) {
								this..cancel();
								this. = null;
							}
							if (idlingStart < 0)
								idlingStart = System.currentTimeMillis();
							else
								notifyIdle(System.currentTimeMillis() - idlingStart);
							this.wait(); // wait until a new job is posted (or reenabled:38901)
else {
							idlingStart = -1;
						}
					}
					if (job == null) {
						notifyIdle(System.currentTimeMillis() - idlingStart);
						// just woke up, delay before processing any new jobs, allow some time for the active thread to finish
						Thread.sleep(500);
						continue;
					}
					if () {
						Util.verbose(awaitingJobsCount() + " awaiting jobs"); //$NON-NLS-1$
						Util.verbose("STARTING background job - " + job); //$NON-NLS-1$
					}
					try {
						this. = true;
						if (this. == null) {
							this. = new ProgressJob(Messages.bind(."""")); //$NON-NLS-1$ //$NON-NLS-2$
							this..setPriority(Job.LONG);
							this..setSystem(true);
							this..schedule();
						}
						/*boolean status = */job.execute(null);
						//if (status == FAILED) request(job);
finally {
						this. = false;
						if ()
							Util.verbose("FINISHED background job - " + job); //$NON-NLS-1$
						if (this. == 0)
							Thread.sleep(50);
					}
catch (InterruptedException e) { // background indexing was interrupted
				}
			}
catch (RuntimeException e) {
			if (this. != null) { // if not shutting down
				// log exception
				Util.log(e"Background Indexer Crash Recovery"); //$NON-NLS-1$
				// keep job manager alive
				this. = null;
				reset(); // this will fork a new thread with no waiting jobs, some indexes will be inconsistent
			}
			throw e;
catch (Error e) {
			if (this. != null && !(e instanceof ThreadDeath)) {
				// log exception
				Util.log(e"Background Indexer Crash Recovery"); //$NON-NLS-1$
				// keep job manager alive
				this. = null;
				reset(); // this will fork a new thread with no waiting jobs, some indexes will be inconsistent
			}
			throw e;
		}
	}
Stop background processing, and wait until the current job is completed before returning
	public void shutdown() {
		if ()
			Util.verbose("Shutdown"); //$NON-NLS-1$
		discardJobs(null); // will wait until current executing job has completed
		Thread thread = this.;
		try {
			if (thread != null) { // see http://bugs.eclipse.org/bugs/show_bug.cgi?id=31858
				synchronized (this) {
					this. = null// mark the job manager as shutting down so that the thread will stop by itself
					notifyAll(); // ensure its awake so it can be shutdown
				}
				// in case processing thread is handling a job
				thread.join();
			}
			Job job = this.;
			if (job != null) {
				job.cancel();
				job.join();
			}
catch (InterruptedException e) {
			// ignore
		}
	}
	public String toString() {
		StringBuffer buffer = new StringBuffer(10);
		buffer.append("Enable count:").append(this.).append('\n'); //$NON-NLS-1$
		int numJobs = this. - this. + 1;
		buffer.append("Jobs in queue:").append(numJobs).append('\n'); //$NON-NLS-1$
		for (int i = 0; i < numJobs && i < 15; i++) {
			buffer.append(i).append(" - job["+i+"]: ").append(this.[this.+i]).append('\n'); //$NON-NLS-1$ //$NON-NLS-2$
		}
		return buffer.toString();
	}
New to GrepCode? Check out our FAQ X