HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Simple Thread Synchronization Tricks"
Vol. 2, Issue 8, p. 36

	

Listing 1: Pipelines gather data from one thread and hand it to another. 

//Java class of the data gathering thread 
class DataGatherer implements Runnable { 
  OutputStream os; 
  public DataGatherer(OutputStream os) { 
    this.os = os; 
    Thread t = new Thread(this); 
    t.start(); 
  } 
  public void run() { 
    while(!done) { 
      //gather data command 

      //write data command using successive calls 
      // to os.write(). 
    } 
    os.close(); 
  } 
} 

//Java class of data processing thread 
class DataProcessor impements Runnable { 
  InputStream is; 
  public DataProcessor(InputStream is) { 
    this.is = is; 
    Thread t = new Thread(this); 
    t.start(); 
  } 
  public void run() { 
    while(!done) { 
      //read single data command using 
      //is.read(). 

      //process command 
    } 
    is.close(); 
  } 
} 

//Main method that sets up the Pipeline. 
public void MyMethod() { 
  PipedOutputStream os = new PipedOutputStream(); 
  PipedInputStream is = new PipedInputStream(); 
  os.connect(is); 
  new DataGatherer(os); 
  new DataProcessor(is); 
} 

Listing 2: Hungry puppies has multiple data processors reading from a single data gatherer.
 
//Java class that gathers data is essentially 
//the same as provided in Listing 1 

//Java class that subclasses PipedInputStream 
//to provide a "readAtomicDataPacket" method 
public class AtomicInputStream extends 
    PipedInputStream { 
  public AtomicInputStream() { 
    super(); 
  } 
  public synchronized Object readAtomicDataPacket() { 
    / read a single command or packet of data 
    //potentially by using multiple calls to 
    //the inherited PipedInputStream.read() 
    //method 
  } 
} 

//Java class that processes data. Multiple 
//objects of this type are created. 
public DataProcessor implements Runnable { 
  AtomicInputStream ais; 
  public DataProcessor(AtomicInputStream ais) { 
    this.ais = ais; 
    Thread t = new Thread(this); 
    t.start(); 
  } 
  public void run() { 
    while(!done) { 
      //Read each atomic command or 
      //data packet from the AtomicInputStream 
      //using its readAtomicDataPacket method 

      //process each data packet individually 
    } 
  } 
} 

Listing 3: Multiple data gathering threads synchronize access to a PipedInputStream that acts as a queue.
 
//Data processing class is essentially the same as that 
//provided in Listing 1 

//AtomicOutputStream is a subclass of PipedOutputStream 
//with a synchronized method to write a single atomic command 
//or data packet. 
public class AtomicOutputStream extends 
    PipedOutputStream { 
  public AtomicOutputStream() { 
    super(); 
  } 
  public synchronized void writeAtomicDataPacket( 
      Object packet) { 
    //write the data represented by the packet object 
    //using multiple calls to the inherited write method 
  } 
} 

//Data gathering class uses an AtomicOutputStream 
//to synchronize writing atomic data packets or commands 
/ to a queue. 
public class DataGatherer implements Runnable { 
  AtomicOutputStream aos; 
  public DataGatherer(AtomicOutputStream aos) { 
    this.aos = aos; 
    Thread t = new Thread(this); 
    t.start(); 
  } 
  public void run() { 
    while(!done) { 
      //Gather a single data packet or command 

      //write the data packet or command to 
      //the AtomicOutputStream using the 
      // AtomicOutputStream.writeAtomicDataPacket 
      //method. 
    } 
  } 
}  


 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.