java multithreading - implement Callable Interface

  • Callable interfaces implement plus generic, i.e. the return value type, default Object
  • call overridden method can have a return value, you can throw an exception

    public class ThreadDownload implements Callable<Boolean> {

    public Boolean call() throws Exception
    {

    return true;

    }
    public static void main(String[]args) throws InterruptedException, ExecutionException
    {
    ThreadDownload a=new ThreadDownload();
    ThreadDownload b=new ThreadDownload();
    ThreadDownload c=new ThreadDownload();
    //创建执行服务
    ExecutorService ser =Executors.newFixedThreadPool(3);
    //提交执行
    Future<Boolean>result1=ser.submit(a);
    Future<Boolean>result2=ser.submit(b);
    Future<Boolean>result3=ser.submit(c);
    //获取结果
    boolean r1=result1.get();
    boolean r2=result2.get();
    boolean r3=result3.get();
    //关闭服务:
    ser.shutdownNow();
    }
    }

Guess you like

Origin blog.51cto.com/14437184/2427274