Callable实现多线程

1.Callable和Runnable

先看一下两个接口的定义:
Callable:

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

Runnable:

interface Runnable {
  public abstract void run();
}

明显能看到区别:
1.Callable能接受一个泛型,然后在call方法中返回一个这个类型的值。而Runnable的run方法没有返回值
2.Callable的call方法可以抛出异常,而Runnable的run方法不会抛出异常。

Future
返回值Future也是一个接口,通过他可以获得任务执行的返回值。

定义如下:

public interface Future<V> {
  boolean cancel(boolean var1);
 
  boolean isCancelled();
 
  boolean isDone();
 
  V get() throws InterruptedException, ExecutionException;
 
  V get(long var1, TimeUnit var3) throws InterruptedException, ExecutionException, TimeoutException;
}

其中的get方法获取的就是返回值。

下面看下提交一个任务的场景:

public class Main {
  public static void main(String[] args) throws InterruptedException, ExecutionException {
  ExecutorService executor = Executors.newFixedThreadPool(2);
  //创建一个Callable,3秒后返回String类型
  Callable myCallable = new Callable() {
    @Override
    public String call() throws Exception {
      Thread.sleep(3000);
      System.out.println("calld方法执行了");
      return "call方法返回值";
    }
  };
  System.out.println("提交任务之前 "+getStringDate());
  Future future = executor.submit(myCallable);
  System.out.println("提交任务之后,获取结果之前 "+getStringDate());
  System.out.println("获取返回值: "+future.get());
  System.out.println("获取到结果之后 "+getStringDate());
  }
  public static String getStringDate() {
    Date currentTime = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
    String dateString = formatter.format(currentTime);
    return dateString;
    }
  }

通过executor.submit提交一个Callable,返回一个Future,然后通过这个Future的get方法取得返回值。

运行结果如下:

提交任务之前 12:13:01
提交任务之后,获取结果之前 12:13:01
calld方法执行了
获取返回值: call方法返回值
获取到结果之后 12:13:04

get()方法的阻塞性

通过上面的输出可以看到,在调用submit提交任务之后,主线程本来是继续运行了。但是运行到future.get()的时候就阻塞住了,一直等到任务执行完毕,拿到了返回的返回值,主线程才会继续运行。

这里注意一下,他的阻塞性是因为调用get()方法时,任务还没有执行完,所以会一直等到任务完成,形成了阻塞。

任务是在调用submit方法时就开始执行了,如果在调用get()方法时,任务已经执行完毕,那么就不会造成阻塞。

下面在调用方法前先睡4秒,这时就能马上得到返回值。

System.out.println("提交任务之前 "+getStringDate());
Future future = executor.submit(myCallable);
System.out.println("提交任务之后 "+getStringDate());
Thread.sleep(4000);
System.out.println("已经睡了4秒,开始获取结果 "+getStringDate());
System.out.println("获取返回值: "+future.get());
System.out.println("获取到结果之后 "+getStringDate());

运行如下:

提交任务之前 12:36:04
提交任务之后 12:36:04
calld方法执行了
已经睡了4,开始获取结果 12:36:08
获取返回值: call方法返回值
获取到结果之后 12:36:08

可以看到吗,因为睡了4秒,任务已经执行完毕,所以get方法立马就得到了结果。

同样的原因,submit两个任务时,总阻塞时间是最长的那个。

下面看下有多个个任务,一个3秒,一个5秒。

Callable myCallable = new Callable() {
  @Override
  public String call() throws Exception {
  Thread.sleep(5000);
  System.out.println("calld方法执行了");
  return "call方法返回值";
  }
};
Callable myCallable2 = new Callable() {
  @Override
  public String call() throws Exception {
  Thread.sleep(3000);
  System.out.println("calld2方法执行了");
  return "call2方法返回值";
  }
};
System.out.println("提交任务之前 "+getStringDate());
Future future = executor.submit(myCallable);
Future future2 = executor.submit(myCallable2);
System.out.println("提交任务之后 "+getStringDate());
System.out.println("开始获取第一个返回值 "+getStringDate());
System.out.println("获取返回值: "+future.get());
System.out.println("获取第一个返回值结束,开始获取第二个返回值 "+getStringDate());
System.out.println("获取返回值2: "+future2.get());
System.out.println("获取第二个返回值结束 "+getStringDate());
提交任务之前 14:14:47
提交任务之后 14:14:48
开始获取第一个返回值 14:14:48
calld2方法执行了
calld方法执行了
获取返回值: call方法返回值
获取第一个返回值结束,开始获取第二个返回值 14:14:53
获取返回值2: call2方法返回值
获取第二个返回值结束 14:14:53

获取第一个结果阻塞了5秒,所以获取第二个结果立马就得到了。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Java 中,可以通过实现 Callable 接口,实现多线程操作。Callable 接口与 Runnable 接口类似,都是用来实现多线程操作的接口。但是,Callable 接口支持返回结果和抛出异常。 下面是一个简单的示例,演示如何使用 Callable 实现多线程操作: ``` import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class MyCallable implements Callable<String> { private String name; public MyCallable(String name) { this.name = name; } @Override public String call() throws Exception { System.out.println("Thread " + name + " is running..."); Thread.sleep(5000); return "Hello from thread " + name; } public static void main(String[] args) throws Exception { ExecutorService executorService = Executors.newFixedThreadPool(3); Future<String> result1 = executorService.submit(new MyCallable("Thread-1")); Future<String> result2 = executorService.submit(new MyCallable("Thread-2")); Future<String> result3 = executorService.submit(new MyCallable("Thread-3")); System.out.println(result1.get()); System.out.println(result2.get()); System.out.println(result3.get()); executorService.shutdown(); } } ``` 在上面的示例中,我们创建了一个实现Callable 接口的类 MyCallable。在 call() 方法中,我们输出了当前线程的名称,然后让线程休眠 5 秒,最后返回一个字符串。 在 main() 方法中,我们创建了一个 ExecutorService,并向其提交了三个 MyCallable 对象。ExecutorService.submit() 方法会返回一个 Future 对象,可以使用 get() 方法获取 MyCallable 对象的返回值。最后,我们关闭了 ExecutorService。 执行上面的代码,可以看到如下输出: ``` Thread Thread-1 is running... Thread Thread-2 is running... Thread Thread-3 is running... Hello from thread Thread-1 Hello from thread Thread-2 Hello from thread Thread-3 ``` 从输出结果可以看出,我们创建的三个线程都在运行,而且每个线程都在休眠 5 秒钟。而且,我们使用 Future 对象获取了每个线程的返回值。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

初夏0811

你的鼓励将是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值