KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > jobs > Worker


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
19  * A worker thread processes jobs supplied to it by the worker pool. When
20  * the worker pool gives it a null job, the worker dies.
21  */

22 public class Worker extends Thread JavaDoc {
23     //worker number used for debugging purposes only
24
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++); //$NON-NLS-1$
30
this.pool = pool;
31         //set the context loader to avoid leaking the current context loader
32
//for the thread that spawns this worker (bug 98376)
33
setContextClassLoader(pool.defaultContextLoader);
34     }
35
36     /**
37      * Returns the currently running job, or null if none.
38      */

39     public Job currentJob() {
40         return (Job) currentJob;
41     }
42
43     private IStatus handleException(InternalJob job, Throwable JavaDoc t) {
44         String JavaDoc 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 JavaDoc e) {
59                     result = handleException(currentJob, e);
60                 } catch (ThreadDeath JavaDoc e) {
61                     //must not consume thread death
62
throw e;
63                 } catch (Error JavaDoc e) {
64                     result = handleException(currentJob, e);
65                 } finally {
66                     //clear interrupted state for this thread
67
Thread.interrupted();
68                     //result must not be null
69
if (result == null)
70                         result = handleException(currentJob, new NullPointerException JavaDoc());
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 JavaDoc t) {
78             t.printStackTrace();
79         } finally {
80             currentJob = null;
81             pool.endWorker(this);
82         }
83     }
84 }
85
Popular Tags