public class Test {
public static void main(String[] args) {
System.out.println("111111111");
Thread c = Thread.currentThread();
System.out.println(c.getName() + "=" + c.getId() + "=" + c.hashCode());
System.out.println("111111111");
final Thread thread = new Thread(new Networker(new CallBackListener() {
@Override
public void onCall() {
// TODO Auto-generated method stub
System.out.println("3333333");
Thread c = Thread.currentThread();
System.out.println(c.getName() + "=" + c.getId() + "="
+ c.hashCode());
System.out.println("3333333");
}
}));
Thread thread2 = new Thread() {
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
System.out.println("55555");
Thread c = Thread.currentThread();
System.out.println(c.getName() + "=" + c.getId() + "="
+ c.hashCode());
System.out.println("55555");
System.out.println("2222");
thread.start();
System.out.println("2222");
}
};
System.out.println("000000");
thread2.start();
System.out.println("000000");
}
}
class Networker implements Runnable {
CallBackListener listener;
public Networker(CallBackListener listener) {
super();
this.listener = listener;
}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("4444");
Thread c = Thread.currentThread();
System.out.println(c.getName() + "=" + c.getId() + "=" + c.hashCode());
System.out.println("4444");
System.out.println("xxxxxxxxxx");
listener.onCall();
System.out.println("xxxxxxxxxx");
}
}
public interface CallBackListener {
public void onCall();
}
再看看执行结果:
证明回调方法是在调用该方法的线程中执行的。
大小: 9.3 KB
分享到:
2013-12-16 19:44
浏览 1669
评论