是的,只要进程仍然存在,您的回调就会运行。
如果你想看看我创建了一个程序来模拟它。它有三个类,只需将它们放在任何一个包中并运行,您就会看到自己。这描绘了即使您的工作线程完成回调仍然来。我希望它有帮助:)
1级
public class Client {
private static long startTime = System.currentTimeMillis();
public static void main(String[] args) {
System.out.println("Time elapsed in ms:" + (System.currentTimeMillis() - startTime));
Client client = new Client();
client.executeJob();
try {
System.out.println("Main thread sleeping");
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Main thread finishing");
}
public void executeJob() {
new Thread() {
@Override
public void run() {
Job job = new Job();
System.out.println("Calling Job with Asyc - Time elapsed in ms:"
+ (System.currentTimeMillis() - startTime));
job.doRequest("Calling Async", new CallBack() {
@Override
public void onRequestComplete(String response) {
System.out.println("Got my callback eventhough job that started the Async is over - Time elapsed in ms:"
+ (System.currentTimeMillis() - startTime));
}
});
System.out.println("Job thread finishing");
}
}.start();
}
}
2 级
public abstract class CallBack {
public abstract void onRequestComplete(String response);
}
3 级
public class Job {
public void doRequest(String request, final CallBack callBack){
new Thread() {
@Override
public void run() {
//Async Long running task
try {
Thread.sleep(10000);
callBack.onRequestComplete("Long Async task Completed-- Sending response back");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
}