Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Observer Pattern - Restart Thread Again When Exception Occurs

Observer Pattern - Restart Thread Again When Exception Occurs

Scheduled Pinned Locked Moved Java
questionregextutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Django_Untaken
    wrote on last edited by
    #1

    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? Example

    static 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 of TaskListener::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 :)

    L 1 Reply Last reply
    0
    • D Django_Untaken

      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? Example

      static 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 of TaskListener::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 :)

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      See java thread exception - Google Search[^].

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups