Bug 223492 - Set worker thread's name to the job's name.

The worker thread name now contains the job name or the the job class
name in case the job name is not set.

Change-Id: Iacce83add56d9c30a1405c069c8ad606b331e7e6
Signed-off-by: Stefan Winkler <stefan@winklerweb.net>
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
diff --git a/bundles/org.eclipse.core.jobs/src/org/eclipse/core/internal/jobs/Worker.java b/bundles/org.eclipse.core.jobs/src/org/eclipse/core/internal/jobs/Worker.java
index 6e13c0b..ef4488b 100644
--- a/bundles/org.eclipse.core.jobs/src/org/eclipse/core/internal/jobs/Worker.java
+++ b/bundles/org.eclipse.core.jobs/src/org/eclipse/core/internal/jobs/Worker.java
@@ -7,6 +7,7 @@
  *
  *  Contributors:
  *     IBM Corporation - initial API and implementation
+ *     Stefan Winkler - Bug 223492: Set current job name as thread name
  *******************************************************************************/
 package org.eclipse.core.internal.jobs;
 
@@ -20,16 +21,18 @@
  * the worker pool gives it a null job, the worker dies.
  */
 public class Worker extends Thread {
-	//worker number used for debugging purposes only
+	// worker number used for debugging purposes only
 	private static int nextWorkerNumber = 0;
 	private volatile InternalJob currentJob;
 	private final WorkerPool pool;
+	private final String generalName;
 
 	public Worker(WorkerPool pool) {
 		super("Worker-" + nextWorkerNumber++); //$NON-NLS-1$
+		this.generalName = getName();
 		this.pool = pool;
-		//set the context loader to avoid leaking the current context loader
-		//for the thread that spawns this worker (bug 98376)
+		// set the context loader to avoid leaking the current context loader
+		// for the thread that spawns this worker (bug 98376)
 		setContextClassLoader(pool.defaultContextLoader);
 	}
 
@@ -53,13 +56,14 @@
 				IStatus result = Status.OK_STATUS;
 				IProgressMonitor monitor = currentJob.getProgressMonitor();
 				try {
+					setName(getJobName());
 					result = currentJob.run(monitor);
 				} catch (OperationCanceledException e) {
 					result = Status.CANCEL_STATUS;
 				} catch (Exception e) {
 					result = handleException(currentJob, e);
 				} catch (ThreadDeath e) {
-					//must not consume thread death
+					// must not consume thread death
 					result = handleException(currentJob, e);
 					throw e;
 				} catch (Error e) {
@@ -68,16 +72,17 @@
 					if (result != Job.ASYNC_FINISH && monitor != null) {
 						monitor.done();
 					}
-					//clear interrupted state for this thread
+					// clear interrupted state for this thread
 					Thread.interrupted();
-					//result must not be null
+					// result must not be null
 					if (result == null) {
 						String message = NLS.bind(JobMessages.jobs_returnNoStatus, currentJob.getClass().getName());
 						result = handleException(currentJob, new NullPointerException(message));
 					}
 					pool.endJob(currentJob, result);
 					currentJob = null;
-					//reset thread priority in case job changed it
+					setName(generalName);
+					// reset thread priority in case job changed it
 					setPriority(Thread.NORM_PRIORITY);
 				}
 			}
@@ -88,4 +93,12 @@
 			pool.endWorker(this);
 		}
 	}
+
+	private String getJobName() {
+		String name = currentJob.getName();
+		if (name == null || name.trim().isEmpty()) {
+			name = "<unnamed job: " + currentJob.getClass().getName() + ">"; //$NON-NLS-1$ //$NON-NLS-2$
+		}
+		return generalName + ": " + name; //$NON-NLS-1$
+	}
 }