------------------------
Callable |
------------------------
# ÊÇÒ»¸ö½Ó¿Ú
# ´´½¨Ö´ÐÐÏ̵߳ķ½Ê½Èý
# Ô´Âë
@FunctionalInterface
public interface Callable {
V call() throws Exception;
}
# ÓëʵÏÖ Runnable Çø±ð
* Óзµ»ØÖµ
* ÊÇÒ»¸ö·ºÐͽӿÚ
* Å׳öÁËÒì³£
* ÐèÒª FutureTask(Future½Ó¿ÚʵÏÖ) Ö§³Ö(ÓÃÓÚ½ÓÊÕÔËËã½á¹û)
# Æäʵ¾ÍÊÇÏ߳̽áÊøµÄʱºò,¿ÉÒÔ»ñÈ¡µ½Ò»¸ö·µ»ØÖµ
# Demo
public class Main implements Callable{
@Override
public Integer call() throws Exception {
Integer sum = 0;
for(int x = 0; x < 10; x++){
sum += x;
}
return sum;
}
public static void main(String[] args) throws Exception{
//´´½¨ callable ʵÀý
Main main = new Main();
//´´½¨ futuretask
FutureTask futureTask = new FutureTask(main);
//Æô¶¯Ïß³Ì
new Thread(futureTask).start();
//µÃµ½½á¹û,»áÒ»Ö±×èÈû,Ö±µ½³ÌÐòÖ´ÐÐÍê³É
Integer result = futureTask.get();
System.out.println(result);
}
}