Using executorService is not an option since I have threads which are not doing similar tasks.
I don't see how this would matter.
Also executorService accepts a thread name prefix instead of a thread name. I want to give each thread a unique name.
So give each thread a name with a ThreadFactory. I have a class which I call a NamedThreadFactory.
I suspect what you would like the thread to do is reflect what the task is doing.
You can do this
ExecutorService es = Executors.newSingleThreadExecutor();
es.submit(new Runnable() {
public void run() {
Thread.currentThread().setName("Working on task A");
try {
System.out.println("Running: "+Thread.currentThread());
} finally {
Thread.currentThread().setName("parked worker thread");
}
}
});
es.shutdown();
prints
Running: Thread[Working on task A,5,main]
BTW There is no point starting a thread and immediately waiting for it to finish. You may as well use the current thread.