Callable接口和Future接口

 

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class CallableAndFuture {
 public static void main(String[] args) throws InterruptedException,
   ExecutionException {
  ExecutorService threadPool = Executors.newSingleThreadExecutor();
  Future<String> future = threadPool.submit(new Callable<String>() {
   public String call() throws Exception {
    Thread.sleep(4000);
    return "hello";
   }
  });
  System.out.println("等待结果。。。。");
  try {
   System.out.println("the return result : "
     + future.get(1, TimeUnit.SECONDS));
  } catch (Exception e) {
   e.printStackTrace();
  }
  System.out.println("-----------------");
  
  
  ExecutorService threadPool2 = Executors.newFixedThreadPool(10);//创建10个指定大小的线程池
  CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(threadPool2);
  for(int i=1;i<=10;i++){
   final int n = i;
   completionService.submit(new Callable<Integer>() {
    public Integer call() throws Exception {
     Thread.sleep(new Random().nextInt(5000));
     return n;
    }
   });
  }
  for(int i=1;i<=10;i++){
   System.out.println(completionService.take().get());//获得最早完成任务的Future。
  }
 }
}

 

 

使用多线程求一个整数以内的所有质数:

import java.util.ArrayList;
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 ZhishuDemo {
 public static List<Integer> zhishu(int start,int end){
  List<Integer> list = new ArrayList<Integer>();
  for(int i=start;i<end;i++){
   boolean isZhishu = true;
   if(i==2){
    list.add(2);
   }
   if(i==3){
    list.add(3);
   }
   for(int j=2;j<Math.sqrt(i)+1;j++){
    if(i%j==0){
     isZhishu = false;
     break;
    }
   }
   if(isZhishu){
    list.add(i);
   }
  }
  return list;
 }
 public static class MyRun implements Runnable{
  int start;
  int end;
  List<Integer> list;
  public MyRun(int start,int end){
   this.start = start;
   this.end = end;
  }
  public void run(){
   list = zhishu(start, end);
  }
 }
 public static class MyCallable implements Callable<List<Integer>>{
  private int end;
  private int begin;
  private List<Integer> list;
  public MyCallable(int begin,int end){
   this.begin = begin;
   this.end = end;
  }
  public List<Integer> call() throws Exception {
   this.list = zhishu(begin, end);
   return list;
  }
 }
 public static void main(String[] args)throws Exception{
  List<Integer> result = new ArrayList<Integer>();
  ExecutorService threadPool = Executors.newCachedThreadPool();
  MyCallable call1 = new MyCallable(1, 5000000);
  MyCallable call2 = new MyCallable(5000000, 10000000);
  Future<List<Integer>> future1 = threadPool.submit(call1);
  Future<List<Integer>> future2 = threadPool.submit(call2);
  result.addAll(future1.get());
  result.addAll(future2.get());
  System.out.println("----------------------------------");
  for(Integer i : result){
   System.out.println(i);
  }
 }
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值