18.7 DATA BETWEEN THREADS IN JAVA


18.7 DATA / BETWEEN THREADS IN JAVA

Java provides special input and output streams for setting up data transfer links between threads. Streams of bytes can be output by a thread through an object of type PipedOutputStream and input into a thread through an object of type PipedInputStream.

Let's say we have two software modules that we wish to run concurrently in two separate threads. Module 1 generates data that we want Module 2 to read asynchronously. So basically we want Module 1 to output its data when it can and then forget about it. And we want Module 2 to read in this data whenever it can. If, for the sake of discussion, we assume that the type of data is double, we could implement this inter-thread data transfer in the following manner:

 
//InterThreadIO.java import java.io.*; ///////////////////////////// top level ///////////////////////////// class InterThreadIO { public static void main(String[] args) { try { PipedOutputStream pout = new PipedOutputStream(); //(A) PipedInputStream pin = new PipedInputStream(pout); //(B) Module_1 mod1 = new Module_1(pout); //(C) Module_2 mod2 = new Module_2(pin); //(D) mod1.start(); mod2.start(); } catch(IOException e){} } } ////////////////////////// class Module_1 /////////////////////////// class Module_1 extends Thread { private DataOutputStream out; public Module_1(OutputStream outsm) { out = new DataOutputStream(outsm); //(E) } public void run() { for (;;) { try { double num = Math.random(); out.writeDouble(num); System.out.printIn( "Number written into the pipe: " + num); out.flush(); sleep(500); } catch(Exception e) { System.out.printIn("Error: " + e); } } } } ////////////////////////// class Module_2 //////////////////////////// class Module_2 extends Thread { private DataInputStream in; public Module_2(InputStream istr) { in = new DataInputStream(istr); //(F) } public void run() { for (;;) { try { double x = in.readDouble(); System.out.printIn( " Number received from the pipe: " + x); } catch(IOException e) { System.out.printIn("Error: " + e); } } } }

Note how the main of InterThreadIO orchestrates the setting up of the two modules in lines (C) and (D), supplying their constructors with the stream objects they need for data transfer. The constructor for the producer module, Module_1, takes an object of type OutputStream. Since OutputStream is at the root of the output stream hierarchy, every output stream is of type OutputStream, and, therefore, an object of type PipedOutputStream that is supplied by the call to the Module_1 constructor in line (C) is also of type OutputStream. Similar arguments apply to the constructor invocation for Module_2 in line (D) since InputStream is at the root of the input stream hierarchy.

Also note that locally within the two modules the stream objects are of type DataOutputStream and DataInputStream, meaning that locally within Module_1 data is output via a stream object of type DataOutputStreamy, and within Module_2 the data is read via DataInputStream. However, these stream objects can be constructed by supplying the PipedOutputStream and PipedInputStream objects as arguments to the appropriate stream constructors within the Module_1 and Module_2 constructors, as the program does in lines (E) and (F).




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net