一 回调函数
采用回调函数,让线程通知主程序何时结束,它通过调用主程序中的一个方法来实现。
(线程就是在该主类中启动的) ,这就称为回调,因为线程在结束的时候,调用其创建者。这样
主程序就可以在线程运行的时候休息,而不会占用线程的时间。
二 线程类实现
在上面代码中的第40行,调用了主类中的一个静态函数 ,而实质上这一静态函数是在线程中运行的。
三 主类中的代码
主类中的main函数实质上只是启动每一个线程,在线程中的静态函数实质上是在thread类中调用
四 总结
回调函数并没有想象中的那么复杂,只是在thread的run方法的结束前,调用主类中定义的方法。
方法需要synchronized 的限制。
采用回调函数,让线程通知主程序何时结束,它通过调用主程序中的一个方法来实现。
(线程就是在该主类中启动的) ,这就称为回调,因为线程在结束的时候,调用其创建者。这样
主程序就可以在线程运行的时候休息,而不会占用线程的时间。
二 线程类实现
1
package
cn.bupt.thread;
2
3 import java.io. * ;
4 import java.security.DigestInputStream;
5 import java.security.MessageDigest;
6 import java.security.NoSuchAlgorithmException;
7
8 import cn.bupt.test.CallbackDigestUserInterface;
9
10 public class ThreadDemo implements Runnable {
11
12 private File input ;
13 private byte [] digest ;
14 public ThreadDemo(File input)
15 {
16 this.input = input ;
17 }
18
19 public byte [] getDigest()
20 {
21 return digest ;
22 }
23
24
25 @Override
26 public void run() {
27
28 try {
29 FileInputStream in = new FileInputStream(input) ;
30 MessageDigest sha = MessageDigest.getInstance("SHA") ;
31 DigestInputStream din = new DigestInputStream(in , sha) ;
32
33 int b ;
34
35 while((b = din.read()) != -1) ;
36 din.close() ;
37
38 digest = sha.digest() ;
39
40 CallbackDigestUserInterface.receiveDigest(digest, input.getName()) ;
41
42 } catch (FileNotFoundException e) {
43 // TODO Auto-generated catch block
44 e.printStackTrace();
45 } catch (NoSuchAlgorithmException e) {
46 // TODO Auto-generated catch block
47 e.printStackTrace();
48 } catch (IOException e) {
49 // TODO Auto-generated catch block
50 e.printStackTrace();
51 }
52
53
54
55
56 }
57
58
59
60}
61
2
3 import java.io. * ;
4 import java.security.DigestInputStream;
5 import java.security.MessageDigest;
6 import java.security.NoSuchAlgorithmException;
7
8 import cn.bupt.test.CallbackDigestUserInterface;
9
10 public class ThreadDemo implements Runnable {
11
12 private File input ;
13 private byte [] digest ;
14 public ThreadDemo(File input)
15 {
16 this.input = input ;
17 }
18
19 public byte [] getDigest()
20 {
21 return digest ;
22 }
23
24
25 @Override
26 public void run() {
27
28 try {
29 FileInputStream in = new FileInputStream(input) ;
30 MessageDigest sha = MessageDigest.getInstance("SHA") ;
31 DigestInputStream din = new DigestInputStream(in , sha) ;
32
33 int b ;
34
35 while((b = din.read()) != -1) ;
36 din.close() ;
37
38 digest = sha.digest() ;
39
40 CallbackDigestUserInterface.receiveDigest(digest, input.getName()) ;
41
42 } catch (FileNotFoundException e) {
43 // TODO Auto-generated catch block
44 e.printStackTrace();
45 } catch (NoSuchAlgorithmException e) {
46 // TODO Auto-generated catch block
47 e.printStackTrace();
48 } catch (IOException e) {
49 // TODO Auto-generated catch block
50 e.printStackTrace();
51 }
52
53
54
55
56 }
57
58
59
60}
61
在上面代码中的第40行,调用了主类中的一个静态函数 ,而实质上这一静态函数是在线程中运行的。
三 主类中的代码
主类中的main函数实质上只是启动每一个线程,在线程中的静态函数实质上是在thread类中调用
1
package
cn.bupt.test;
2
3 import java.io.File;
4
5 import cn.bupt.thread.ThreadDemo;
6
7 public class CallbackDigestUserInterface {
8
9 public static void receiveDigest(byte [] digest , String name){
10
11 StringBuffer result = new StringBuffer(name) ;
12 result.append(": ") ;
13 for(int j = 0 ; j < digest.length ; j++){
14 result.append(digest[j] + " ") ;
15 }
16 System.out.println("result :" + result) ;
17 }
18 /** *//**
19 * @param args
20 */
21 public static void main(String[] args) {
22 // TODO Auto-generated method stub
23 for(int i = 0 ; i < args.length ; i++)
24 {
25 File f = new File(args[i]) ;
26 ThreadDemo td = new ThreadDemo(f) ;
27 Thread t = new Thread(td) ;
28 t.start() ;
29 }
30 }
31}
32
2
3 import java.io.File;
4
5 import cn.bupt.thread.ThreadDemo;
6
7 public class CallbackDigestUserInterface {
8
9 public static void receiveDigest(byte [] digest , String name){
10
11 StringBuffer result = new StringBuffer(name) ;
12 result.append(": ") ;
13 for(int j = 0 ; j < digest.length ; j++){
14 result.append(digest[j] + " ") ;
15 }
16 System.out.println("result :" + result) ;
17 }
18 /** *//**
19 * @param args
20 */
21 public static void main(String[] args) {
22 // TODO Auto-generated method stub
23 for(int i = 0 ; i < args.length ; i++)
24 {
25 File f = new File(args[i]) ;
26 ThreadDemo td = new ThreadDemo(f) ;
27 Thread t = new Thread(td) ;
28 t.start() ;
29 }
30 }
31}
32
四 总结
回调函数并没有想象中的那么复杂,只是在thread的run方法的结束前,调用主类中定义的方法。
方法需要synchronized 的限制。