Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/eclipse/core/internal/jobs/Worker.java


1   /*******************************************************************************
2    * Copyright (c) 2003, 2004 IBM Corporation and others.
3    * All rights reserved. This program and the accompanying materials 
4    * are made available under the terms of the Common Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/cpl-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.InternalPlatform;
14  import org.eclipse.core.internal.runtime.Policy;
15  import org.eclipse.core.runtime.*;
16  import org.eclipse.core.runtime.jobs.Job;
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 {
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    }
32  
33    /**
34     * Returns the currently running job, or null if none.
35     */
36    public Job currentJob() {
37      return (Job) currentJob;
38    }
39  
40    private IStatus handleException(InternalJob job, Throwable t) {
41      String message = Policy.bind("jobs.internalError", job.getName()); //$NON-NLS-1$
42      return new Status(IStatus.ERROR, Platform.PI_RUNTIME, Platform.PLUGIN_ERROR, message, t);
43    }
44  
45    private void log(IStatus result) {
46      try {
47        InternalPlatform.getDefault().log(result);
48      } catch (RuntimeException e) {
49        //failed to log, so print to console instead
50        Throwable t = result.getException();
51        if (t != null)
52          t.printStackTrace();
53      }
54    }
55  
56    public void run() {
57      setPriority(Thread.NORM_PRIORITY);
58      try {
59        while ((currentJob = pool.startJob(this)) != null) {
60          //if job is null we've been shutdown
61          if (currentJob == null)
62            return;
63          currentJob.setThread(this);
64          IStatus result = Status.OK_STATUS;
65          try {
66            result = currentJob.run(currentJob.getProgressMonitor());
67          } catch (OperationCanceledException e) {
68            result = Status.CANCEL_STATUS;
69          } catch (Exception e) {
70            result = handleException(currentJob, e);
71          } catch (Error e) {
72            result = handleException(currentJob, e);
73          } finally {
74            //clear interrupted state for this thread
75            Thread.interrupted();
76            //result must not be null
77            if (result == null)
78              result = handleException(currentJob(), new NullPointerException());
79            pool.endJob(currentJob, result);
80            if ((result.getSeverity() & (IStatus.ERROR | IStatus.WARNING)) != 0)
81              log(result);
82            currentJob = null;
83          }
84        }
85      } catch (Throwable t) {
86        t.printStackTrace();
87      } finally {
88        currentJob = null;
89        pool.endWorker(this);
90      }
91    }
92  }