Callable和Future

  

 Callable和Future

Callable and Future, they are very interesting, one produces the result, and the other gets the result. 

The Callable interface is similar to Runnable, as can be seen from the name, but Runnable does not return results, and cannot throw exceptions that return results, while Callable is more powerful. After being executed by a thread, it can return a value. This return value can be Get by Future, that is to say, Future can get the return value of asynchronously executed task,

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(){

       // maybe do something

       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");

}

 

}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326902043&siteId=291194637