home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 August / PCO0897.ISO / filesbbs / os2 / fp1os2.arj / OS2 / DATA / 49 / C / 0 / F_26836 / SJMTObject.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-05-15  |  4.6 KB  |  205 lines

  1. import stardiv.tools.SjProtocolWindow;
  2.  
  3. public class SJMTObject implements Runnable, SJMessageHandlerInterface {
  4.    protected Thread aThread;
  5.    private SJEventSemaphore aSem;
  6.    protected JSbxQueue aMessageQueue;
  7.    private SJMessageHandlerInterface aCallbackObj;
  8.    private boolean bStopMessageLoop;
  9.    private boolean bThreadIsReady;
  10.    private static long nObjCount;
  11.  
  12.    protected void finalize() {
  13.       SjProtocolWindow.DecSJMTObjectCount();
  14.       if (SjProtocolWindow.IsMemTraceModeOn()) {
  15.          System.out.println(--nObjCount + "***DESTR. " + this);
  16.       }
  17.  
  18.    }
  19.  
  20.    protected void Constr() {
  21.       SjProtocolWindow.IncSJMTObjectCount();
  22.       if (SjProtocolWindow.IsMemTraceModeOn()) {
  23.          System.out.println(++nObjCount + "***CONSTR. " + this);
  24.       }
  25.  
  26.    }
  27.  
  28.    public SJMTObject() {
  29.       this(true, true, (SJMessageHandlerInterface)null, (String)null, (ThreadGroup)null);
  30.    }
  31.  
  32.    public SJMTObject(SJMessageHandlerInterface var1) {
  33.       this(true, true, var1, (String)null, (ThreadGroup)null);
  34.    }
  35.  
  36.    public SJMTObject(boolean var1, String var2, ThreadGroup var3) {
  37.       this(var1, var1, (SJMessageHandlerInterface)null, var2, var3);
  38.    }
  39.  
  40.    public SJMTObject(SJMessageHandlerInterface var1, String var2, ThreadGroup var3) {
  41.       this(true, true, var1, var2, var3);
  42.    }
  43.  
  44.    public SJMTObject(SJMessageHandlerInterface var1, String var2) {
  45.       this(true, true, var1, var2, (ThreadGroup)null);
  46.    }
  47.  
  48.    public SJMTObject(boolean var1) {
  49.       this(var1, true, (SJMessageHandlerInterface)null, (String)null, (ThreadGroup)null);
  50.    }
  51.  
  52.    public SJMTObject(SJMessageHandlerInterface var1, boolean var2) {
  53.       this(var2, true, var1, (String)null, (ThreadGroup)null);
  54.    }
  55.  
  56.    private SJMTObject(boolean var1, boolean var2, SJMessageHandlerInterface var3, String var4, ThreadGroup var5) {
  57.       this.Constr();
  58.       this.bStopMessageLoop = false;
  59.       this.bThreadIsReady = false;
  60.       this.aCallbackObj = var3;
  61.       if (var1) {
  62.          this.aMessageQueue = new JSbxQueue(this);
  63.          if (var5 != null && var4 != null) {
  64.             this.aThread = new Thread(var5, this, var4);
  65.          }
  66.  
  67.          if (var5 != null && var4 == null) {
  68.             this.aThread = new Thread(var5, this);
  69.          }
  70.  
  71.          if (var5 == null && var4 != null) {
  72.             this.aThread = new Thread(this, var4);
  73.          }
  74.  
  75.          if (var5 == null && var4 == null) {
  76.             this.aThread = new Thread(this);
  77.          }
  78.  
  79.          if (var2) {
  80.             this.aThread.start();
  81.             return;
  82.          }
  83.       } else {
  84.          this.aMessageQueue = null;
  85.          this.aThread = null;
  86.       }
  87.  
  88.    }
  89.  
  90.    public final boolean IsRunningInOwnThread() {
  91.       return this.aThread != null;
  92.    }
  93.  
  94.    public final void DoStartThread() {
  95.       if (this.aThread != null) {
  96.          this.aThread.start();
  97.       }
  98.  
  99.    }
  100.  
  101.    public final void DoSuspendThread() {
  102.       if (this.aThread != null) {
  103.          this.aThread.suspend();
  104.       }
  105.  
  106.    }
  107.  
  108.    public final void DoResumeThread() {
  109.       if (this.aThread != null) {
  110.          this.aThread.resume();
  111.       }
  112.  
  113.    }
  114.  
  115.    public final void DoStopThread() {
  116.       if (this.aThread != null) {
  117.          this.aThread.stop();
  118.       }
  119.  
  120.    }
  121.  
  122.    protected void ProcessMessage(SJMessage var1) {
  123.       System.out.println("WARNING: SJMTObject.ProcessMessage() not overloaded ! this=" + this);
  124.    }
  125.  
  126.    public void HandleMessage(SJMessage var1) {
  127.       if (this.IsRunningInOwnThread()) {
  128.          if (this.ExistsMessageQueue()) {
  129.             this.aMessageQueue.Append(var1);
  130.             return;
  131.          }
  132.       } else {
  133.          this.ProcessMessage(var1);
  134.       }
  135.  
  136.    }
  137.  
  138.    public void run() {
  139.       this.SetThreadReady();
  140.       if (this.aCallbackObj != null) {
  141.          this.aCallbackObj.HandleMessage(new SJMessage(10021, (SJMessageHandlerInterface)null, this));
  142.       }
  143.  
  144.       while(!this.bStopMessageLoop) {
  145.          SJMessage var1 = (SJMessage)this.aMessageQueue.Get();
  146.          this.ProcessMessage(var1);
  147.       }
  148.  
  149.       if (this.aCallbackObj != null) {
  150.          this.aCallbackObj.HandleMessage(new SJMessage(10023, (SJMessageHandlerInterface)null, this));
  151.       }
  152.  
  153.       this.ClearThreadReady();
  154.    }
  155.  
  156.    public final synchronized void StopMessageLoop() {
  157.       this.bStopMessageLoop = true;
  158.       this.AddToMessageQueue(new SJMessage());
  159.       SjProtocolWindow.AddNotifyEvent(this, "SJMTObject.StopMessageLoop()");
  160.       this.notifyAll();
  161.       SjProtocolWindow.AddNotifyEventDONE(this, "SJMTObject.StopMessageLoop()");
  162.    }
  163.  
  164.    public final boolean ExistsMessageQueue() {
  165.       return this.aMessageQueue != null;
  166.    }
  167.  
  168.    public final void AddToMessageQueue(SJMessage var1) {
  169.       if (this.aMessageQueue != null) {
  170.          this.aMessageQueue.Append(var1);
  171.       }
  172.  
  173.    }
  174.  
  175.    public final boolean IsThreadReady() {
  176.       return this.bThreadIsReady;
  177.    }
  178.  
  179.    public final synchronized void WaitForThreadReady() {
  180.       try {
  181.          while(!this.bThreadIsReady) {
  182.             SjProtocolWindow.AddWaitEvent(this, "SJMTObject.WaitForThreadReady()");
  183.             this.wait();
  184.             SjProtocolWindow.AddWaitEventDONE(this, "SJMTObject.WaitForThreadReady()");
  185.          }
  186.  
  187.       } catch (InterruptedException var1) {
  188.       }
  189.    }
  190.  
  191.    protected final synchronized void SetThreadReady() {
  192.       this.bThreadIsReady = true;
  193.       SjProtocolWindow.AddNotifyEvent(this, "SJMTObject.SetThreadReady()");
  194.       this.notifyAll();
  195.       SjProtocolWindow.AddNotifyEventDONE(this, "SJMTObject.SetThreadReady()");
  196.    }
  197.  
  198.    protected final synchronized void ClearThreadReady() {
  199.       this.bThreadIsReady = false;
  200.       SjProtocolWindow.AddNotifyEvent(this, "SJMTObject.ClearThreadReady()");
  201.       this.notifyAll();
  202.       SjProtocolWindow.AddNotifyEventDONE(this, "SJMTObject.ClearThreadReady()");
  203.    }
  204. }
  205.