Created
January 23, 2017 02:18
-
-
Save 0532/ffa580d0ec6adf68d4042f774b73df43 to your computer and use it in GitHub Desktop.
回调
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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