欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Callable和Future

程序员文章站 2022-05-29 13:37:11
...

  

 Callable和Future

Callable和Future,它俩很有意思的,一个产生结果,一个拿到结果。 

Callable接口类似于Runnable,从名字就可以看出来了,但是Runnable不会返回结果,并且无法抛出返回结果的异常,而Callable功能更强大一些,被线程执行后,可以返回值,这个返回值可以被Future拿到,也就是说,Future可以拿到异步执行任务的返回值,

public class Test extends Object{

 

/**

* @param args

*/

public static void main(String[] args) {

ExecutorService threadPool = Executors.newCachedThreadPool();

        final CompletionService<Integer> cs = new ExecutorCompletionService<Integer>(threadPool);

        for(int i = 1; i < 5; i++) {

            final int taskID = i;

            cs.submit(new Callable<Integer>() {

                public Integer call() throws Exception {

                Thread.sleep(1000);

                    return taskID;

                }

            });

        }

        System.out.println(" ***");

        new Thread(){

        public void run(){

       // 可能做一些事情

       for(int i = 1; i < 5; i++) {

           try {

               System.out.println(cs.take().get());

           } catch (InterruptedException e) {

               e.printStackTrace();

           } catch (ExecutionException e) {

               e.printStackTrace();

           }

       }

        }

        }.start();

        System.out.print(" pong");

}

void c(){

ExecutorService threadPool = Executors.newSingleThreadExecutor();

        Future<Integer> future = threadPool.submit(new Callable<Integer>() {

            public Integer call() throws Exception {

            Thread.sleep(1000);

                return new Random().nextInt(100);

            }

        });

        

         future = threadPool.submit(new Callable<Integer>() {

            public Integer call() throws Exception {

            Thread.sleep(1000);

            int i=new Random().nextInt(100);

            System.out.println(i);

                return i;

            }

        });

        

         future = threadPool.submit(new Callable<Integer>() {

            public Integer call() throws Exception {

            Thread.sleep(1000);

                return new Random().nextInt(100);

            }

        });

        

        System.out.print(" pong");

}

void b(){

for(int i=0;i<5;i++){

Callable<String> c = new Callable<String>(){

public String call() throws Exception {

Thread.currentThread().sleep(2000);

System.out.println("call");

return String.valueOf(new Random().nextInt(100));

}

};

FutureTask<String> future = new FutureTask<String>(c);

       new Thread(future).start();

       

       try {

Thread.currentThread().sleep(1000);

String s;

s = future.get();

System.out.println(s);

}

       catch (InterruptedException e) {

System.out.println("**1**");

e.printStackTrace();

}catch (ExecutionException e) {

e.printStackTrace();

}

}

System.out.print(" pong");

}

void a(){

//int i=0,j,k=0;

//for(j=0;i<6 && j<10;i++,j++){

//k=i+j;

//}

//System.out.println("i="+i + ",j="+j);

 

Callable<String> c = new Callable<String>(){

public String call() throws Exception {

Thread.currentThread().sleep(2000);

System.out.println("call");

return String.valueOf(new Random().nextInt(100));

}

};

FutureTask<String> future = new FutureTask<String>(c);

       new Thread(future).start();

       

       try {

Thread.currentThread().sleep(1000);

String s;

s = future.get();

System.out.println(s);

}

       catch (InterruptedException e) {

System.out.println("**1**");

e.printStackTrace();

}catch (ExecutionException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

/*new Thread(){

public void run() {

 

System.out.print("ping");

}

}.start();

try {

Thread.currentThread().sleep(1000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.print(" pong");

*/

System.out.print(" pong");

}

 

}

相关标签: java