001 
002 /**
003  * Title:        Advanced Network Client Sample<p>
004  * Description:  <p>
005  * Copyright:    Copyright (C) 2009 Alexey Veremenko<p>
006  * Company:      <p>
007  @author Alexey Veremenko
008  @version 1.0
009  */
010 package networking.base;
011 
012 /**
013  * ForkedThread - this class represents a thread which can be forked
014  */
015 abstract class ForkedThread extends Thread
016 {
017     /**
018      * Fork status NONE neither parent nor child
019      */
020     protected static final int NONE = 0;
021 
022     /**
023      * Fork status CHILD - a thread was forked as child
024      */
025     protected static final int CHILD = 1;
026 
027     /**
028      * Fork status PARENT - a thread has forked other thread and become parent
029      */
030     protected static final int PARENT = 2;
031 
032     /**
033      * Fork status
034      */
035     protected int m_fork = NONE;
036 
037     /**
038      * Construct new ForkedThread object
039      @param forkStatus initial fork status
040      */
041     public ForkedThread()
042     {
043     }
044 
045     /**
046      * Convenience routine
047      * @throw Exception
048      */
049     protected void run1() throws Exception
050     {
051     }
052 
053     /**
054      * Allocate any required resources
055      */
056     protected void init() throws Exception
057     {
058     }
059 
060     /**
061      * Deallocate resources
062      */
063     protected void deinit() throws Exception
064     {
065     }
066 
067     /*
068      * Override Thread
069      */
070     public void run()
071     {
072         try
073         {
074             if (m_fork != CHILD)
075                 init();
076 
077             // Encapsulate inner errors
078             try
079             {
080                 run1();
081             }
082             catch (Exception e)
083             {
084                 error(e);
085             }
086 
087             if (m_fork != PARENT)
088                 deinit();
089         }
090         catch (Exception e)
091         {
092             error(e);
093         }
094     }
095 
096     /**
097      * Report error
098      @param e exception
099      */
100     abstract protected void error(Exception e);
101 }
Java2html