Using Messaging Threads


To create effective SmartSockets applets, it is usually necessary to spin off a new messaging thread. This is simply a run method in some class that operates while the applet is allowed to execute. This thread loops, doing a TipcSrv.mainLoop call, allowing SmartSockets message processing to occur.

For example, the typical messaging thread infrastructure looks something like:

public class MyApplet extends Applet 
  implements Runnable, ... {  
 
    Thread reader = null; 
 
  // messaging thread       
    public void run() { 
      Thread me = Thread.currentThread(); 
      me.setPriority(Thread.MIN_PRIORITY); 
    while (reader == me) { 
      try { 
        TipcSrv srv = TipcSvc.getSrv(); 
        srv.mainLoop(1.0); 
      } catch (TipcException te) { } 
      try { 
        Thread.sleep(100); 
      } catch (InterruptedException ie) { 
        break;  
      } // catch 
    } // messaging (reader) thread 
  } // run 
 
  public void stop() { 
    reader = null; 
  } // stop 
 
  public void start() { 
    reader = new Thread(this); 
    reader.start(); 
  } // start 
 
  . 
  . 
  . 
} // class 

The files for this lesson are located in the directories:

Windows:
%RTHOME%\java\tutorial\lesson6 
UNIX:
$RTHOME/java/tutorial/lesson6 

These files all contain code similar to the above. Note that changing the priority of the messaging thread is not necessary, but may help in some situations. Due to the scheduler inconsistencies between "green threads" and native threads, and native threads on differing platforms (Solaris versus Windows NT specifically), some experimentation may occasionally be necessary to achieve balanced messaging and CPU utilization.


TIBCO SmartSockets™ Java Library User’s Guide and Tutorial
Software Release 6.8, July 2006
Copyright © TIBCO Software Inc. All rights reserved
www.tibco.com