Java Reference
In-Depth Information
following the flow of control from the main() method. Instead, it is invoked by each
thread separately. That is, receiveDigest() runs inside the digesting threads rather
than inside the main thread of execution.
Example 3-6. CallbackDigestUserInterface
import javax.xml.bind.* ; // for DatatypeConverter; requires Java 6 or JAXB 1.0
public class CallbackDigestUserInterface {
public static void receiveDigest ( byte [] digest , String name ) {
StringBuilder result = new StringBuilder ( name );
result . append ( ": " );
result . append ( DatatypeConverter . printHexBinary ( digest ));
System . out . println ( result );
}
public static void main ( String [] args ) {
for ( String filename : args ) {
// Calculate the digest
CallbackDigest cb = new CallbackDigest ( filename );
Thread t = new Thread ( cb );
t . start ();
}
}
}
Examples 3-5 and 3-6 use static methods for the callback so that CallbackDigest only
needs to know the name of the method in CallbackDigestUserInterface to call.
However, it's not much harder (and it's considerably more common) to call back to an
instance method. In this case, the class making the callback must have a reference to
the object it's calling back. Generally, this reference is provided as an argument to the
thread's constructor. When the run() method is nearly done, the last thing it does is
invoke the instance method on the callback object to pass along the result. For instance,
Example 3-7 shows a CallbackDigest class that is much the same as before. However,
it now has one additional field, an InstanceCallbackDigestUserInterface object
called callback . At the end of the run() method, the digest is passed to callback 's
receiveDigest() method. The InstanceCallbackDigestUserInterface object itself
is set in the constructor.
Example 3-7. InstanceCallbackDigest
import java.io.* ;
import java.security.* ;
public class InstanceCallbackDigest implements Runnable {
private String filename ;
private InstanceCallbackDigestUserInterface callback ;
Search WWH ::




Custom Search