1 11 package org.eclipse.core.internal.jobs; 12 13 import org.eclipse.core.internal.runtime.RuntimeLog; 14 import org.eclipse.core.runtime.*; 15 import org.eclipse.core.runtime.jobs.Job; 16 import org.eclipse.osgi.util.NLS; 17 18 22 public class Worker extends Thread { 23 private static int nextWorkerNumber = 0; 25 private volatile InternalJob currentJob; 26 private final WorkerPool pool; 27 28 public Worker(WorkerPool pool) { 29 super("Worker-" + nextWorkerNumber++); this.pool = pool; 31 setContextClassLoader(pool.defaultContextLoader); 34 } 35 36 39 public Job currentJob() { 40 return (Job) currentJob; 41 } 42 43 private IStatus handleException(InternalJob job, Throwable t) { 44 String message = NLS.bind(JobMessages.jobs_internalError, job.getName()); 45 return new Status(IStatus.ERROR, JobManager.PI_JOBS, JobManager.PLUGIN_ERROR, message, t); 46 } 47 48 public void run() { 49 setPriority(Thread.NORM_PRIORITY); 50 try { 51 while ((currentJob = pool.startJob(this)) != null) { 52 currentJob.setThread(this); 53 IStatus result = Status.OK_STATUS; 54 try { 55 result = currentJob.run(currentJob.getProgressMonitor()); 56 } catch (OperationCanceledException e) { 57 result = Status.CANCEL_STATUS; 58 } catch (Exception e) { 59 result = handleException(currentJob, e); 60 } catch (ThreadDeath e) { 61 throw e; 63 } catch (Error e) { 64 result = handleException(currentJob, e); 65 } finally { 66 Thread.interrupted(); 68 if (result == null) 70 result = handleException(currentJob, new NullPointerException ()); 71 pool.endJob(currentJob, result); 72 if ((result.getSeverity() & (IStatus.ERROR | IStatus.WARNING)) != 0) 73 RuntimeLog.log(result); 74 currentJob = null; 75 } 76 } 77 } catch (Throwable t) { 78 t.printStackTrace(); 79 } finally { 80 currentJob = null; 81 pool.endWorker(this); 82 } 83 } 84 } 85 | Popular Tags |