Observer Pattern - Restart Thread Again When Exception Occurs
-
Hello there. I am trying to implement
Observer Pattern
. I get notified, successfully, whenever a thread completes. I can start the same thread again when I get notified (through TaskListener I have implemented). But how do I start it again if there was some exception? Examplestatic MyWork objMyWork = null; // implements Runnable
static Thread objThread = null;public static void main(String[] args) throws
{
TaskListener listener = new TaskListener()
{
@Override
public void threadComplete()
{
try
{
objMyWork = new MyWork();
objMyWork.SetData1(int data);
objMyWork.SetData2(String data);
objMyWork.addListener(this);objThread = new Thread(objMyWork); objThread.start(); objThread.join(); } catch(Exception ex) { /\* HERE I WANT TO START THREAD AGAIN \*/ } } } objMyWork = new MyWork(); objMyWork.SetData1(int data); objMyWork.SetData2(String data); objMyWork.addListener(listener); objThread = new Thread(objMyWork); objThread.start(); objThread.join();
}
1- What changes do I make in
MyWork
so that control always comes back in the catch section ofTaskListener::threadComplete()
whenever there was exception? 2- Once control is there, how do I start this thread again (I dont want to nest another try-catch block and then another in the nested one and then another .... and so on). Thanks for anything you share :) -
Hello there. I am trying to implement
Observer Pattern
. I get notified, successfully, whenever a thread completes. I can start the same thread again when I get notified (through TaskListener I have implemented). But how do I start it again if there was some exception? Examplestatic MyWork objMyWork = null; // implements Runnable
static Thread objThread = null;public static void main(String[] args) throws
{
TaskListener listener = new TaskListener()
{
@Override
public void threadComplete()
{
try
{
objMyWork = new MyWork();
objMyWork.SetData1(int data);
objMyWork.SetData2(String data);
objMyWork.addListener(this);objThread = new Thread(objMyWork); objThread.start(); objThread.join(); } catch(Exception ex) { /\* HERE I WANT TO START THREAD AGAIN \*/ } } } objMyWork = new MyWork(); objMyWork.SetData1(int data); objMyWork.SetData2(String data); objMyWork.addListener(listener); objThread = new Thread(objMyWork); objThread.start(); objThread.join();
}
1- What changes do I make in
MyWork
so that control always comes back in the catch section ofTaskListener::threadComplete()
whenever there was exception? 2- Once control is there, how do I start this thread again (I dont want to nest another try-catch block and then another in the nested one and then another .... and so on). Thanks for anything you share :)