Returns information from the end of the thread, noting that the run () method and the Start () method do not return any values. can be implemented with ' callback '!
The case is as follows (see Java Network programming 3th):
Package callback;/* * How do I get thread output? * 1. Directly using the access method such as get () to get the thread output, this method will be inconsistent with the main thread and other threads, * the main function in the use of objects returned in the thread, the object may not have been initialized in the threads. * You can use polling (while (xxx!= NULL)) test. * 2. Callback. When the thread's Run method approaches the end, a known method in the main class is called based on the result, which can either be a static method that invokes the main class, or an instance method can be invoked in the thread with the instances of the main class (which are held by the threading class, which can be passed through the constructor). * The callback mechanism can handle more complex situations involving more threads, objects, and classes. * For example, there are multiple objects that care about the results of a thread, then a thread can hold a list of callback objects. * An object is added to the list by calling thread or a method of the Runnable class to indicate that it is interested in the results of the calculation. * # If there are instances of multiple classes that care about the results, you can define a interface (interface) so that all of these classes implement this interface. * #下面的DigestListener接口就是这样一个例子 */public interface Digestlistener {//This interface declares the Digestcalculated method, This method will act as a callback method in the thread public void digestcalculated (byte[] digest);}
Package Callback;import Java.io.*;import Java.security.digestinputstream;import java.security.messagedigest;import Java.util.list;import java.util.listiterator;import java.util.vector;/* * Calculate the file summary of the runnable classes */public class Listcallbackdigest implements Runnable {Private file input;//file source list listennerlist=new Vector ();//lists that hold callback objects public Listcallbackdigest (File input) {this.input=input;} -------adding objects to the list of callback objects (implementing the Digestlistener Interface)---------public synchronized void Adddigestlistenner (Digestlistener l) {Listennerlist.add (l);} Public synchronized void Removedigestlistenner (Digestlistener l) {listennerlist.remove (L);} ----------------sends a callback request to all the callback objects stored in the list-----------------private synchronized void Senddigest (byte[] Digest) { Listiterator Itrator=listennerlist.listiterator (); while (Itrator.hasnext ()) {Digestlistener dl= (DigestListener) Itrator.next ();//Call a callback function for each callback object, return information from the thread to the target object, and the target object in this example mainly completes the calculation of the file Digest dl.digestcalculated (Digest);}} public void Run () {try {fileinputstream in=new fileinputstream (input); MessagedigEST sha=messagedigest.getinstance ("sha");D Igestinputstream din=new digestinputstream (in, SHA); int B;while (b= Din.read ())!=-1);//constantly read din.close (); byte[] Digest=sha.digest (); This.senddigest (Digest);//thread Last Call callback method, return information} catch ( Exception e) {e.printstacktrace ();}}}
Package Callback;import java.io.*;p ublic class UserInterface implements digestlistener{public static long beginTime; Private file Input;private byte[] digest;//Save line path back Data public userinterface (File input) {this.input=input;} --------method that actually starts the thread------------public void calculatdigest (int i) {listcallbackdigest lcb=new listcallbackdigest ( input); Lcb.adddigestlistenner (this);//Add current object thread T=new thread (LCB); T.setname ("Thread" + (i+1)); T.start ();} @Override//------------callback method-------------public void digestcalculated (byte[] Digest) {//TODO auto-generated method Stubthis.digest=digest; System.out.print (this);//The ToString method of the class is rewritten as long endTime = System.currenttimemillis ();//record current system time SYSTEM.OUT.PRINTLN (Thread.CurrentThread (). GetName () + ":" + "time consuming" + (Endtime-begintime)/1000+ "seconds");} Public String toString () {string res=input.getname () + ":", if (digest!=null) {for (int i=0;i<digest.length;i++) {res+= Digest[i]+ "";}} elseres+= "Digest not available!"; return res;} -------------main function---------------public static void main (string[] args) {beginTime = System.currenttimemillis ();//record current system time for (int i=0;i<args.length;i++) {File f=new file (Args[i]); UserInterface u=new userinterface (f); u.calculatdigest (i); System.out.println ("Main () End!" ");}}
can refer to the previous article http://blog.csdn.net/hellozpc/article/details/42027539
Returns information from a thread (callback)