什么是可调用Java中? [英] What is callable in Java?

查看:160
本文介绍了什么是可调用Java中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题pretty多少资金了。

我想知道的可调用的概念和想法。我读了<一个href=\"http://stackoverflow.com/questions/141284/the-difference-between-the-runnable-and-callable-interfaces-in-java\">question这里可调用和可运行的差异。但没有人秀code,并给予详细的可调用的是什么。我不想知道它们之间的区别。我想知道,


  1. 什么是可调用的?


  2. 在使用它们以及如何使用它们。


  3. 当他们在行动
        Android系统。



解决方案

您可以检查此的例如的:

在这个例子中可调用任务返回线程一秒后,执行任务的名称。我们使用执行人框架并行执行100任务和使用预订来获得的提交的任务的结果。

 包com.journaldev.threads;进口的java.util.ArrayList;
进口java.util.Date;
进口的java.util.List;
进口java.util.concurrent.Callable;
进口java.util.concurrent.ExecutionException;
进口java.util.concurrent.ExecutorService中;
进口java.util.concurrent.Executors;
进口java.util.concurrent.Future;公共类MyCallable实现可赎回&LT;串GT; {    @覆盖
    公共字符串调用()抛出异常{
        视频下载(1000);
        //返回线程名执行此任务可赎回
        返回Thread.currentThread()的getName()。
    }    公共静态无效的主要(字符串ARGS []){
        //从执行者工具类获取ExecutorService的,线程池的大小为10
        ExecutorService的执行人= Executors.newFixedThreadPool(10);
        //创建一个列表将与相关的可调用Future对象
        清单&LT;&未来LT;弦乐&GT;&GT;名单=新的ArrayList&LT;&未来LT;弦乐&GT;&GT;();
        //创建MyCallable实例
        可赎回&LT;串GT;赎回=新MyCallable();
        的for(int i = 0; I&LT; 100;我++){
            //提交赎回的任务由线程池来执行
            未来&LT;串GT;未来= executor.submit(赎回);
            //添加未来到列表中,我们可以用未来获取收益的价值
            list.add(未来);
        }
        对于(未来&LT;串GT; FUT:名单){
            尝试{
                //打印未来的返回值,注意在控制台中的输出延迟
                //因为的Future.get()等待任务完成获得
                的System.out.println(新的Date()+::+ fut.get());
            }赶上(InterruptedException的|为ExecutionException E){
                e.printStackTrace();
            }
        }
        //现在关闭执行人服务
        executor.shutdown();
    }}

您还可以检查 <一个href=\"https://blogs.oracle.com/CoreJavaTechTips/entry/get_netbeans_6?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A%20corejavatechtips%20(Core%20Java%20Technologies%20Tech%20Tips)\"相对=nofollow>使用可调用的返回结果从的Runnable

The title pretty much sums it.

I want to know the concept and idea of callable . I have read a question here on difference between callable and runnable. but no one show code and give detail what a callable is. I don't want to know the difference between them. I want to know ,

  1. What is a callable ?

  2. When to use them and how to use them .

  3. When they come in action for Android.

解决方案

You can check this example:

In this example Callable task returns the name of thread executing the task after one second. We are using Executor framework to execute 100 tasks in parallel and use Future to get the result of the submitted tasks.

package com.journaldev.threads;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class MyCallable implements Callable<String> {

    @Override
    public String call() throws Exception {
        Thread.sleep(1000);
        //return the thread name executing this callable task
        return Thread.currentThread().getName();
    }

    public static void main(String args[]){
        //Get ExecutorService from Executors utility class, thread pool size is 10
        ExecutorService executor = Executors.newFixedThreadPool(10);
        //create a list to hold the Future object associated with Callable
        List<Future<String>> list = new ArrayList<Future<String>>();
        //Create MyCallable instance
        Callable<String> callable = new MyCallable();
        for(int i=0; i< 100; i++){
            //submit Callable tasks to be executed by thread pool
            Future<String> future = executor.submit(callable);
            //add Future to the list, we can get return value using Future
            list.add(future);
        }
        for(Future<String> fut : list){
            try {
                //print the return value of Future, notice the output delay in console
                // because Future.get() waits for task to get completed
                System.out.println(new Date()+ "::"+fut.get());
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        }
        //shut down the executor service now
        executor.shutdown();
    }

}

You may also check Using Callable to Return Results From Runnables

这篇关于什么是可调用Java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆