Skip to content

Instantly share code, notes, and snippets.

@0532
Created January 23, 2017 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0532/ffa580d0ec6adf68d4042f774b73df43 to your computer and use it in GitHub Desktop.
Save 0532/ffa580d0ec6adf68d4042f774b73df43 to your computer and use it in GitHub Desktop.
回调
/**
* Created By WangLichao On 2016年12月26日.
*/
public class ListableFutureTest {
private ExecutorService executor = Executors.newFixedThreadPool(1);
private ListeningExecutorService service = MoreExecutors.listeningDecorator(executor);
public static void main(String[] args){
ListableFutureTest test = new ListableFutureTest();
ListenableFuture<Student> future = test.service.submit(new Callable<Student>() {
@Override
public Student call() throws Exception {
Thread.sleep(1000*5);
return new Student("张三", "男");
}
});
Futures.addCallback(future, new FutureCallback<Student>() {
@Override
public void onSuccess(@Nullable Student result) {
System.out.println("执行结果"+result);
}
@Override
public void onFailure(Throwable t) {
System.out.println("error=============");
}
});
System.out.println("abcdd");
}
}
class Student{
private String name;
private String sex;
public Student() {}
public Student(String name, String sex) {
super();
this.name = name;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment