Callable和Future

一直想讓子線程跑起來後將結果傳遞給主線程,在對Runable失望後,最終找到了Callable和Future這兩個接口,完美知足了個人需求。 最大的區別就是Runable返回void,而Callable返回V或者異常。線程

JDK中描述以下:orm

A task returns a result and may throw an exception. Implementors define a single method with no arguments called call.接口

The Callable interface is simillar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. A Runnable, however, does not return a result and cannot throw a checked exception.it

The Executors class contains utility methods to convert from other common forms to Callable classes.io

相關文章
相關標籤/搜索