
本文共 3559 字,大约阅读时间需要 11 分钟。
������������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
1.1 ������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������������A���������a()���������B���������b()���������a()���������������������b()���������������������������������������������������b()���������������������������������������������������������������������b()������������������������������������a()������������������������������������������������������������������������������
1.2 ������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������������������������������������������������������������������������������������������������������������������������Java������Future���Callable������������������������������������������������
1.3 ������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������������������������������������������������������A���������a()���������B���������b()������B���������b()���������������������������������A���callback()������������������������������������������������������������������������������������
- ������������
- ���������������������
- ������������������
- ���������������������
- ������������������������
- ������������
���������������������������������������������tellAnswer(int answer)������������������������������������������������������������������������������������������������������
public interface Callback { public void tellAnswer(int answer);}
public class Teacher implements Callback { private Student student; public Teacher(Student student) { this.student = student; } public void askQuestion() { student.resolveQuestion(this); } @Override public void tellAnswer(int answer) { System.out.println("���������������������������������" + answer); }}
public class Student { public void resolveQuestion(Callback callback) { // ��������������������������� try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } // ������������������������ callback.tellAnswer(10080); // ���������10080��������������������������������������������������������� }}
public class CallbackTest { @Test public void testCallback() { Student student = new Student(); Teacher teacher = new Teacher(student); teacher.askQuestion(); }}
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Student������������List������������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������"this"���������������������������������������������������������������
发表评论
最新留言
关于作者
