In java, what are the suggested ways to implement the two thread requirements
- I would like the name a thread
- I would like the parent (or main) thread know if there are any exceptions by this child thread.
For 2, I understand that having a future object would be a better approach. So to implement the above two requirements below is a code I came up with
class MyRunnable implements Runnable {
....
}
MyRunnable myRunnable = new MyRunnable ();
FutureTask futureTask = new FutureTask(myRunnable, null);
Thread myThread = new MyThread(futureTask, "processing-thread");
myThread.start();
try {
futureTask.get();
} catch (Exception e) {
throw new RuntimeException(e)
}
This seems to be a lot of code for simple stuff. Any better ways?
Note - Using executorService is not an option since I have threads which are not doing similar tasks. Also executorService accepts a thread name prefix instead of a thread name. I want to give each thread a unique name.