使用executor、callable以及一个Future 计算欧拉数e
package test; import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
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 CaculateE {
final static int num=17;
public static void main(String[] args) {
// TODO Auto-generated method stub
//使用线程池防止多条人物提交而创建的线程
ExecutorService executor=Executors.newFixedThreadPool(1);
Callable<BigDecimal> callable=new Callable<BigDecimal>() { @Override
public BigDecimal call() throws Exception {
// TODO Auto-generated method stub
//舍位模式,四舍五入,100为e的精度上限
MathContext mc=new MathContext(100,RoundingMode.HALF_UP);
BigDecimal result=BigDecimal.ZERO;
//e=1/0!+1/1!+1/2!+1/3!+...+...
for(int i=0;i<num;i++) {
//factory()表示阶乘的结果 new BigDecimal() 确保结果精确,防止精度丢失
BigDecimal factorial=factory(new BigDecimal(i));
System.out.println("factorial:"+factorial);
//1除以factorial的阶乘
BigDecimal res=BigDecimal.ONE.divide(factorial,mc);
System.out.println("res:"+res);
result=result.add(res);
System.out.println("result:"+result);
}
return result;
}
//不用该方法进行转换会报执行异常
private BigDecimal factory(BigDecimal n) {
// TODO Auto-generated method stub
System.err.println("n的值为:"+n);
if(n.equals(BigDecimal.ZERO)) { return BigDecimal.ONE;
}
else {
//subtract(n.subtract(BigDecimal.ONE)) 对象中的值相减
return n.multiply(factory(n.subtract(BigDecimal.ONE)));}
} }; //提交一个runable任务进行执行,同时返回一个代表此任务的Future实例
Future<BigDecimal> taskFuture =executor.submit(callable);
try {
//判断任务是否完成,模拟任务执行,未完成输出waiting,完成之后直接输出结果
while(!taskFuture.isDone())
System.out.println("waiting/");
//线程执行之后可以获取结果
//Callable接口代表一段可以调用并返回结果的代码;Future接口表示异步任务,是还没有完成的任务给出的未来结果。所以说Callable用于产生结果,Future用于获取结果。
System.out.println("taskFuture的get()方法"+taskFuture.get()); }catch(ExecutionException e){
System.err.println("哦豁,抛出执行异常了!");
}catch (InterruptedException ie) {
// TODO Auto-generated catch block
ie.printStackTrace();
System.err.println("interrupted while waiting");
}
executor.shutdown();
}
}
代码执行流程:
main()方法调用Excutors的newFixedThreadPool方法获取一个executor.之后初始化实现了Callable接口的匿名类并且将这个任务提交给executor,在返回接收一个Future的实例。
通过isDone()判断是否执行完毕,’waiting‘表示一直在执行,当执行完毕,直接通过 taskFuture.get()获取计算的结果。
阶乘求值过程(包含一个递归):
return n.multiply(factory(n.subtract(BigDecimal.ONE)));
例如:5*factory(4)
=>5*4*factory(3)
=>5*4*3*factory(2)
=>5*4*3*2*factory(1)
=>5*4*3*2*1*factory(0)
=>120
| i | 0 | 1 | 2 | 3 | 4 | 5 | 6 | ... | 16 |
| 返回值 | factor(0) | 1*factory(0) | 2*factory(1) | 3*factory(2) | 4*factory(3) | 5*factory(4) | 6*factory(5) | ... | 16*factory(15) |
| factorial | factory(0) | 1*factory(0) | 2*1*factory(0) | 3*2*1*factory(0) | 4*3*2*factory(0) | 5*4*3*2*factory(0) | 6*5*4*3*2*factory(0) | ... | 16*15*..*2*factory(0) |
| factorial | 1 | 1 | 2 | 6 | 12 | 60 | 120 | ... | 16*15*..*2*factory(0) |
| res | 1/1 | 1/1+1/1 | 1/1+1/1+1/2 | 1/1+1/1+1/2+1/6 | 1/1+1/1+1/2+1/6+1/12 | 1/1+1/1+1/2+1/6+1/12+1/60 | 1/1+1/1+1/2+1/6+1/12+1/60+1/120 | ... | 1/1+...+1/16*15*..*2*1 |
ps:增加精度和num的值以求更长的收敛时间和更为接近的e
使用executor、callable以及一个Future 计算欧拉数e的更多相关文章
- Java线程池 / Executor / Callable / Future
为什么需要线程池? 每次都要new一个thread,开销大,性能差:不能统一管理:功能少(没有定时执行.中断等). 使用线程池的好处是,可重用,可管理. Executor 4种线程 ...
- Callable接口和Future
本篇说明的是Callable和Future,它俩很有意思的,一个产生结果,一个拿到结果. Callable接口类似于Runnable,从名字就可以看出来了,但是Runnable不会返回结 ...
- 给出两个单词word1和word2,写一个函数计算出将word1 转换为word2的最少操作次数。
问题: 给出两个单词word1和word2,写一个函数计算出将word1 转换为word2的最少操作次数. 你总共三种操作方法: 1.插入一个字符 2.删除一个字符 3.替换一个字符 格式: 输入行输 ...
- LINUX上一个命令计算PI
Linux上一个命令计算PI – 笑遍世界 http://smilejay.com/2017/11/calculate-pi-with-linux-command/ [root@d1 goEcho]# ...
- 并发编程之Callable异步,Future模式
Callable 在Java中,创建线程一般有两种方式,一种是继承Thread类,一种是实现Runnable接口.然而,这两种方式的缺点是在线程任务执行结束后,无法获取执行结果.我们一般只能采用共享变 ...
- Java Callable接口与Future接口的两种使用方式
Java Callable.Future的两种使用方式Callable+Futurepublic class Test { public static void main(String[] args) ...
- Callable接口使用以及计算斐波那契数字的数值总和
一.简单使用 Runnable是执行工作的独立任务,但是它不返回任何值.如果你希望任务完成的时能够返回一个值,那么可以实现一个Callable接口.在Java SE5中引入的Callable是一种具有 ...
- Callable抛出异常与future.get
public class ThreadPoolTest { @Test public void testException(){ try{ testExecutorServiceException() ...
- 怎么用wait、notify巧妙的设计一个Future模式?
我们知道多线程可以实现同时执行多个任务(只是看起来是同时,其实是CPU的时间片切换特别快我们没感觉而已). 现在假设一个做饭的场景,你没有厨具也没有食材.你可以去网上买一个厨具,但是这段时间,你不需要 ...
随机推荐
- Docker实现GPA+Exporter监控告警系统
Docker实现GPA+Exporter监控告警系统 1.搭建grafana,prometheus,blackbox_exporter环境 # docker run -d -p 9090:9090 - ...
- pdo类的使用
使用方法 2.php <?php require_once "./mypdo.php"; $pdo = DAOPDO::getInstance('localhost', 'r ...
- [leetcode] 309. Best Time to Buy and Sell Stock with Cooldown(medium)
原题 思路: 状态转移 出售股票的状态,最大利润有两种可能. 一,和昨天一样不动:二,昨天持有的股票今天卖掉. sell[i] = max(sell[i-1],buy[i-1] + prices[i] ...
- C-哈夫曼编码
/*author:windy_2*/ /*修正版*/ #include<stdio.h> #include<stdlib.h> #include<string.h> ...
- Python -----函数(基础部分)
函数: 1.定义: 函数是对功能的封装 2.语法: def 函数名 函数体 函数名 函数名的命名规则和变量一样 3.函数的返回值: return,函数执行完毕,不会执行后面的 1.如果函数中不写ret ...
- iView学习笔记(一):Table基本操作(包含前后端示例代码)
iView表格操作 1.前端准备工作 首先新建一个项目,然后引入iView插件,配置好router npm安装iView npm install iview --save cnpm install i ...
- C#七种设计原则
在C#中有七种设计原则 分别是 1.开闭原则(Open-Closed Principle, OCP) 2.单一职责原则(Single Responsibility Principle) 3.里氏替换原 ...
- 国内CDH的MAVEN代理
在编译CDH版本的各个开源软件时,需要从cdh-repo下载对应的jar包,但发现下载速度非常慢,甚至有时候出现下载异常的情况. 下面是国内可用的.速度非常快的一个maven代理仓库,亲测可用: ht ...
- 利用ImageAI库只需几行python代码超简实现目标检测
目录 什么是目标检测 目标检测算法 Two Stages One Stage python实现 依赖 安装 使用 附录 什么是目标检测 目标检测关注图像中特定的物体目标,需要同时解决解决定位(loca ...
- CentOS yum 源修改
修改 CentOS 默认 yum 源为 mirrors.163.com 首先备份系统自带yum源配置文件/etc/yum.repos.d/CentOS-Base.repo [root@localhos ...