Multithreading and callback

Posted by Bogart on Wed, 23 Oct 2019 22:26:54 +0200

Original link: http://www.cnblogs.com/1q2w3e/archive/2012/06/11/2544546.html

I have been working for a year, but I haven't used many threads. It's so weak. I have a look today. I wrote a small example. Forget about it later. The main task is to monitor the return value of the thread. I.e. no rotation training and variable setting. Let the thread inform the main class (I have finished the work for you.)  

Code it directly.  

  1.  package com.bjz;  
  2.   
  3. /** 
  4.  * Realization 
  5.  * @author bjz 
  6.  * 
  7.  */  
  8. public interface CallbackListener {  
  9.     public void srtartCallbackThread();  
  10.   
  11.     public void print(String returnResult);  //Main implementation method, why not red???  
  12.   
  13. }  


Interface as long as it is a class implementation for calling thread. So as to realize the decoupling of thread and calling method (ps: thread to main class)

  1. package com.bjz;  
  2.   
  3. public class Reciver implements CallbackListener {  
  4.     private String num;  
  5.     private int count;  
  6.   
  7.     String result;  
  8.   
  9.     public Reciver(String num, int count) {  
  10.         this.num = num;  
  11.         this.count = count;  
  12.     }  
  13.   
  14.     @Override  
  15.     public void srtartCallbackThread() {  
  16.         CallbackAndThread callbackAndThread = new CallbackAndThread(num, count);  
  17.         callbackAndThread.addList(this);  
  18.         new Thread(callbackAndThread).start();  
  19.           
  20.     }  
  21.   
  22.     [color=darkred]@Override  
  23.     public void print(String returnResult) {  
  24.         this.result = returnResult;  
  25.         System.out.println(result);  
  26.     }[/color]   //Main implementation method, why not red???  
  27. }  


Down is the thread

  1. package com.bjz;  
  2.   
  3. import java.util.List;  
  4. import java.util.ListIterator;  
  5. import java.util.Vector;  
  6.   
  7. public class CallbackAndThread implements Runnable {  
  8.   
  9.     private String note;  
  10.     private int count;  
  11.     private List listenerList = new Vector();  
  12.   
  13.     public CallbackAndThread(String note, int count) {  
  14.         this.note = note;  
  15.         this.count = count;  
  16.     }  
  17.   
  18.     public void addList(CallbackListener listener) {  
  19.         listenerList.add(listener);  
  20.     }  
  21.   
  22.     public void removeList(CallbackListener listener) {  
  23.         listenerList.remove(listener);  
  24.     }  
  25. [color=darkred]  
  26.     public synchronized void returnResult(String note, int count) {  
  27.         ListIterator iterator = listenerList.listIterator();  
  28.         while (iterator.hasNext()) {  
  29.             CallbackListener listener = (CallbackListener) iterator.next();  
  30.             StringBuffer sb = new StringBuffer();  
  31.             for (int i = 0; i < count; i++) {  
  32.                 sb.append(note);  
  33.             }  
  34.             listener.print(sb.toString());  
  35.         }  
  36.     }[/color]   //Main implementation method, why not red???  
  37.   
  38.     @Override  
  39.     public void run() {  
  40.         returnResult(note, count);  
  41.     }  
  42. }  

 

  1. package com.bjz;  
  2.   
  3. public class Test {  
  4.     public static void main(String[] args) {  
  5.         for (int i=0;i<10;i++) {  
  6.             Reciver reciver = new Reciver(i+""50);  
  7.             reciver.srtartCallbackThread();  
  8.         }  
  9.     }  
  10. }  


Test class

Reproduced in: https://www.cnblogs.com/1q2w3e/archive/2012/06/11/25445446.html

Topics: Java