实现多线程的四种方式和Runnable与Callable的区别

实现多线程的四种方式

继承 Thread 类,重写 run 方法
public class TestExtendsThread extends Thread {

  public static void main(String[] args) {
    TestExtendsThread thread1 = new TestExtendsThread();
    thread1.start();
  }

  @Override
  public void run() {
    System.out.println("继承了 Thread 类");
  }
}
实现 Runnable 接口,实现 run 方法
public class TestImplRunnable implements Runnable {

  public static void main(String[] args) {
    TestImplRunnable runnable = new TestImplRunnable();
    Thread thread = new Thread(runnable);
    thread.start();
  }


  @Override
  public void run() {
    System.out.println("实现了 Runnable 接口");
  }
}
Callable 和 FutureTask
public class TestImplCallable implements Callable<String> {

  public static void main(String[] args) {
    TestImplCallable callable = new TestImplCallable();
    FutureTask<String> futureTask = new FutureTask<>(callable);
    Thread thread = new Thread(futureTask);
    thread.start();
  }

  @Override
  public String call() throws Exception {
    System.out.println("Callable 和 FutureTask");
    return null;
  }
}
线程池
public class TestExecutor implements Runnable {

  public static void main(String[] args) {
    ExecutorService executor = Executors.newFixedThreadPool(2);
    TestExecutor runnable = new TestExecutor();
    executor.execute(runnable);
    executor.shutdown();
  }

  @Override
  public void run() {
    System.out.println("线程池");
  }
}

Runnable 和 Callable 的区别

首先来看看 Runnable 和 Callable 这两个接口。

// Runnable
public interface Runnable {
    public abstract void run();
}

// Callable
public interface Callable<V> {
    V call() throws Exception;
}

相较于 Callable 可以发现 Runnable 接口的 run 方法是没有返回值的,并且也不会抛出任何异常,由此可以得出两个关键不同点:

  1. Callable 能返回执行结果,Runnable 是不能返回结果的。
  2. Callable 中的 call 方法允许抛出异常;而 Runnable 接口的 run 方法的异常只能在内部消化,不能继续上抛。

获取 Callable 返回值 Demo

通过调用 FutureTaskget 方法,阻塞获取返回结果。

public class TestImplCallable implements Callable<String> {

  public static void main(String[] args) throws ExecutionException, InterruptedException {
    TestImplCallable callable = new TestImplCallable();
    FutureTask<String> futureTask = new FutureTask<>(callable);
    Thread thread = new Thread(futureTask);
    thread.start();
    System.out.println(futureTask.get());
  }

  @Override
  public String call() throws Exception {
    Thread.sleep(1000);
    return "test return value";
  }
}

控制台打印结果

test return value

处理 Callable 抛出的异常 Demo

在执行过程中抛出的异常,会被包装到 ExecutionException 中,所以我们只需要从获取到原始的异常就好了。

public class TestImplCallable implements Callable<String> {

  public static void main(String[] args) {
    TestImplCallable callable = new TestImplCallable();
    FutureTask<String> futureTask = new FutureTask<>(callable);
    Thread thread = new Thread(futureTask);
    thread.start();
    try {
      System.out.println(futureTask.get());
    } catch (InterruptedException e) {
      e.printStackTrace();
    } catch (ExecutionException e) {
      System.out.println(e.getCause());
    }
  }

  @Override
  public String call() throws Exception {
    throw new RuntimeException("执行过程中发生异常");
  }
}

控制台打印结果

java.lang.RuntimeException: 执行过程中发生异常
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值