加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Runable与Callable的区别

发布时间:2020-12-15 07:56:30 所属栏目:Java 来源:网络整理
导读:Runable与Callable的区别: public interface CallableV { V call() throws Exception; // V是Callable返回值的类型 } public interface Runnable { public abstract void run();} Runable与Callable 相同点: 1、两者都是接口; 2、两者都可用来编写多线程程
Runable与Callable的区别:
public interface Callable<V> {
    V call() throws Exception;//V是Callable返回值的类型
}
public interface Runnable {
    public abstract void run();
}
Runable与Callable相同点:
1、两者都是接口;
2、两者都可用来编写多线程程序;
3、两者都需要调用Thread.start()启动线程;
不同点:
1、实现Callable接口的任务线程能返回执行结果,此时需要调用FutureTask.get()方法实现,此方法会阻塞主线程直到获取call()返回的结果;当不调用此方法时,主线程不会阻塞;而实现Runnable接口的任务线程不能返回结果;
2、Callable接口的call()方法允许抛出异常;而Runnable接口的run()方法的异常只能在内部消化,不能继续上抛;
?
?
Callable的3种启动线程的方式
1、Thread启动
public class CallableImpl implements Callable<String> {
    private String acceptStr;
    public CallableImpl(String acceptStr) {
        this.acceptStr = acceptStr;
    }
    @Override
    public String call() throws Exception {
        // 任务等待1 秒
        Thread.sleep(1000);
        return this.acceptStr + ",hello!";
    }
    public static void main(String[] args) throws ExecutionException,InterruptedException {
        //Callable<String> callable = new CallableImpl("afei");
        CallableImpl impl=new CallableImpl ("afei");
        FutureTask<String> task = new FutureTask<String>(impl);
        long beginTime = System.currentTimeMillis();
        // 创建线程
        new Thread(task).start();
        // 调用get()阻塞主线程,反之,线程不会阻塞
        String result = task.get();//阻塞主线程,直至1s后获取到call()的返回内容
        System.out.println("hello : " + result);//返回:afei,hello!
    }
}
2、ExecutorService 启动
public class CallableImpl implements Callable<String> {
    private String acceptStr;
    public CallableImpl(String acceptStr) {
        this.acceptStr = acceptStr;
    }
    @Override
    public String call() throws Exception {
        // 任务等待1 秒
        Thread.sleep(1000);
        return this.acceptStr + ",hello!";
    }
 public static void main(String[] args) throws ExecutionException,InterruptedException {
        ExecutorService exec = Executors.newCachedThreadPool(); // 创建线程池
        // 向里面扔任务并执行该任务,同时返回一个包含call()方法返回值的Featured对象
        CallableImpl impl=new CallableImpl(“afei");
        Future<String> future=exec.submit(impl); 
        System.out.println(future.get()); //打印线程(任务)执行的结果
        exec.shutdown();// 关闭线程池后不接受新任务,已经在线程池的任务会被执行完
    }
}
3、controler方法中直接返回Callable对象或其派对象
@RestController
public class IAsyncController {
    private Logger logger=LoggerFactory.getLogger(getClass());
@PostMapping(
"/async/back") public Callable<String> back(){ logger.info("主线程开始"); Callable<String> c=new Callable<String>() { @Override public String call() throws Exception { // TODO Auto-generated method stub logger.info("副线程开始等待1s..."); Thread.sleep(1000); logger.info("副线程结束等待"); return "afei"; } }; logger.info("主线程结束并返回"); return c;//启动副线程并返回call()的返回值 } }

?

?

Runnable的3种启动线程的方式:
1、继承Thread类
public class Test extends Thread{
  public void run(){
   //操作临界资源
  }
   public static void main(String[] args){
       Test t=new Test();
       t.start();//启动线程
  } 
}
2、继承Runnable接口
public class Test implements Runnable{
  public void run(){
   //操作临界资源
  }
   public static void main(String[] args){
        Test t=new Test(); 
        Thread thread=new Thread(t); 
        thread.start();//启动线程
  } 
}
3、无名线程
public class Test{
   public static void main(String[] args){
       new Thread(()->{  //启动一个线程
            System.out.pritln("启动一个线程);
        }).start();
  } 
}    

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读