java asynchronous callback Comments

After Yang to paint the walls, but do not want to please labor, so she called on students to help her paint told the students took the key to a house painter, brushing tell her the results

Contact between a teacher and students: callback interfaces

2, teachers must implement the callback interface so that students can find teachers through this contact

3, the teacher asked the students to get a reference, so that you can help students register callback interface

4, students must know who to contact, that is, must pass callback interface as the incoming parameters, in addition, you must also know what the task is to arrange the teacher, so tasks as to the Senate

Come what ~ on the code!

Zhu ~~~~~

1, callback interfaces

public interface Callback {
     void process(String result);
}

2, the class teacher

public  class TeacherYang the implements the Callback { 

    Private StudentHe studentHe; 

    public TeacherYang (StudentHe studentHe) {
         the this .studentHe = studentHe; 
    } 

    public  void shuaQiang ( Final String houseLocation) { 
     System.out.println ( "Yang students how to brush circulated wall House position, location is: "+ houseLocation); 

     new new the Thread ( new new the Runnable () { 
         @Override 
         public  void RUN () { 
// here callback function is registered, the registration TeacherYang.this callback function to achieve studentHe.getHouseLocation (TeacherYang.
the this , houseLocation); } .}) Start (); System.out.println ( "what the students have received the transmitted position of the teacher, preparing brush!" ); } @Override public void Process (String Result) { the try { thread.sleep ( . 5 * 1000 ); } the catch (InterruptedException E) { e.printStackTrace (); } System.out.println ( "home tea ... Yang students how to receive information sent by:" + result + "Yang satisfied smile, I thought: a better and save money ..." ); } }

3, student class

public  class StudentHe { 

    public  void getHouseLocation (Callback callback, String houseLocation) { 
       System.out.println ( "What the students have received the position of teacher sent, the location is:" + houseLocation + "-----> started to work! " ); 

       the try { 
           the Thread.sleep ( 10 * 1000 ); 
       } the catch (InterruptedException E) { 
           e.printStackTrace (); 
       } 
       System.out.println ( " brush 3 hours, tired like a dog, where students finally brushing "! ); 
       String Status =" 'teacher brushing the wall, you can live!' " ; 
       callback.process (Status); 
    } 

}

4, the test class

public class CallBackTest {
    public static void main(String[] args) {
        StudentHe studentHe = new StudentHe();
        TeacherYang teacherYang = new TeacherYang(studentHe);
        teacherYang.shuaQiang("湖南大学家属院");
    }
}

5 results

Young teacher gives the students how to brush the wall house position, position: Hunan University family member courtyard 
where students have received the position of teacher sent position: Hunan University family member courtyard -----> started to work! 
It sent the students have received the position of teacher, ready to open brush! 
Brush three hours, tired like a dog, what the students finally brushing! 
Yang at home drinking tea ... how students receive information sent by: 'teacher brushing the wall, you can live! 'Yang satisfied smile, I thought: a better and save money ...

 

Guess you like

Origin www.cnblogs.com/mmh760/p/10950325.html
Recommended