资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Callable接口和Runnable接口

Java代码  

创新互联服务项目包括萍乡网站建设、萍乡网站制作、萍乡网页制作以及萍乡网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,萍乡网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到萍乡省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

public interface Executor {  

  

    /** 

     * Executes the given command at some time in the future.  The command 

     * may execute in a new thread, in a pooled thread, or in the calling 

     * thread, at the discretion of the Executor implementation. 

     * 

     * @param command the runnable task 

     * @throws RejectedExecutionException if this task cannot be 

     * accepted for execution. 

     * @throws NullPointerException if command is null 

     */  

    void execute(Runnable command);  

}  

 

Java代码  

//接口ExecutorService继承自Executor,它的目的是为我们管理Thread对象,从而简化并发编程  

public interface ExecutorService extends Executor {  

  

    Future submit(Callable task);  

     

    Future submit(Runnable task, T result);  

  

    Future submit(Runnable task);  

      

    ...     

}  

 

Java代码  

public interface Callable {  

    /** 

     * Computes a result, or throws an exception if unable to do so. 

     * 

     * @return computed result 

     * @throws Exception if unable to compute a result 

     */  

    V call() throws Exception;  

}  

  

  

public interface Runnable {  

     

    public abstract void run();  

}  

 

Java代码  

public interface Future {  

      

    boolean cancel(boolean mayInterruptIfRunning);      

  

    /** 

     * Waits if necessary for the computation to complete, and then 

     * retrieves its result. 

     * 

     * @return the computed result   

     */  

    V get() throws InterruptedException, ExecutionException;  

  

     

    V get(long timeout, TimeUnit unit)  

        throws InterruptedException, ExecutionException, TimeoutException;  

}  

 

Callable接口和Runnable接口相似,区别就是Callable需要实现call方法,而Runnable需要实现run方法;并且,call方法还可以返回任何对象,无论是什么对象,JVM都会当作Object来处理。但是如果使用了泛型,我们就不用每次都对Object进行转换了。

 

Runnable和Callable都是接口

不同之处:

1.Callable可以返回一个类型V,而Runnable不可以

2.Callable能够抛出checked exception,而Runnable不可以。

3.Runnable是自从java1.1就有了,而Callable是1.5之后才加上去的

4.Callable和Runnable都可以应用于executors。而Thread类只支持Runnable.

上面只是简单的不同,其实这两个接口在用起来差别还是很大的。Callable与executors联合在一起,在任务完成时可立刻获得一个更新了的Future。而Runable却要自己处理

 

  Future接口,一般都是取回Callable执行的状态用的。其中的主要方法:

cancel,取消Callable的执行,当Callable还没有完成时

get,获得Callable的返回值

isCanceled,判断是否取消了

isDone,判断是否完成

 

用Executor来构建线程池,应该要做的事:

1).调用Executors类中的静态方法newCachedThreadPool(必要时创建新线程,空闲线程会被保留60秒)或newFixedThreadPool(包含固定数量的线程池)等,返回的是一个实现了ExecutorService接口的ThreadPoolExecutor类或者是一个实现了ScheduledExecutorServiece接口的类对象。

2).调用submit提交Runnable或Callable对象。

3).如果想要取消一个任务,或如果提交Callable对象,那就要保存好返回的Future对象。

4).当不再提交任何任务时,调用shutdown方法。

 

举2个例子如下:

Java代码   

package thread.test04;  

import java.util.concurrent.*;  

public class ThreadTestA {  

    public static void main(String[] args) {  

        ExecutorService e=Executors.newFixedThreadPool(10);  

        e.execute(new MyRunnableA());  

        e.execute(new MyRunnableB());  

       e.shutdown();  

   }  

  

}  

  

class MyRunnableA implements Runnable{  

      

    public void run(){  

        System.out.println("Runnable:run()....");  

        int i=0;  

        while(i<20){  

            i++;  

            for(int j=0;j<1000000;j++);  

            System.out.println("i="+i);  

        }  

    }  

}  

  

class MyRunnableB implements Runnable{  

    public void run(){  

        char c='A'-1;  

        while(c<'Z'){  

            c++;  

            for(int j=0;j<1000000;j++);  

            System.out.println("c="+c);  

        }  

    }  

}  

 

Java代码   

package thread.test04;  

  

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 ThreadTestB {  

    public static void main(String[] args) {  

        ExecutorService e=Executors.newFixedThreadPool(10);  

        Future f1=e.submit(new MyCallableA());  

        Future f2=e.submit(new MyCallableA());  

        Future f3=e.submit(new MyCallableA());        

        System.out.println("--Future.get()....");  

        try {  

            System.out.println(f1.get());  

            System.out.println(f2.get());  

            System.out.println(f3.get());            

        } catch (InterruptedException e1) {  

            e1.printStackTrace();  

        } catch (ExecutionException e1) {  

            e1.printStackTrace();  

        }  

          

        e.shutdown();  

          

    }  

  

}  

  

class MyCallableA implements Callable{  

    public String call() throws Exception {  

        System.out.println("开始执行Callable");  

        String[] ss={"zhangsan","lisi"};  

        long[] num=new long[2];  

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

            num[(int)(Math.random()*2)]++;  

        }  

          

        if(num[0]>num[1]){  

            return ss[0];  

        }else if(num[0]

            throw new Exception("弃权!");  

        }else{  

            return ss[1];  

        }  

    }  

      

}  


网站标题:Callable接口和Runnable接口
网页链接:http://cdkjz.cn/article/gigpsj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220