分布與并行計算—并行計算π(Java)

并行計算π

public class pithread extends Thread {private static long mini=1000000000;private long start,diff;double sum=0;double cur=1/(double)mini;public pithread(long start,long diff) {this.start=start;this.diff=diff;}@Overridepublic void run() {long i=start;for(;i<mini;i+=diff){double x=(i+0.5)*cur;sum+=4/(1+x*x);}}public double getSum() {return sum;}public static void main(String[] args) {double t1,t2;pithread thread0=new pithread(1,1);t1=System.currentTimeMillis();thread0.start();try {thread0.join();} catch (InterruptedException e) {e.printStackTrace();}t2=System.currentTimeMillis();System.out.println("單線程時間:"+(double)(t2-t1)/1000+'秒');pithread thread5=new pithread(1,2);pithread thread6=new pithread(2,2);t1=System.currentTimeMillis();thread5.start();thread6.start();try {thread5.join();} catch (InterruptedException e) {e.printStackTrace();}t2=System.currentTimeMillis();System.out.println("2線程時間:"+(double)(t2-t1)/1000+'秒');pithread thread=new pithread(1,4);pithread thread2=new pithread(2,4);pithread thread3=new pithread(3,4);pithread thread4=new pithread(4,4);t1=System.currentTimeMillis();thread.start();thread2.start();thread3.start();thread4.start();try {thread.join();} catch (InterruptedException e) {e.printStackTrace();}t2=System.currentTimeMillis();System.out.println("4線程時間:"+(double)(t2-t1)/1000+'秒');}
}

并行計算多位π

public class CalculatePThread {static BigDecimal tempt = new BigDecimal(0);public synchronized static void  add(BigDecimal bigDecimal){tempt= tempt.add(bigDecimal);}public static void main(String[] args) {ExecutorService executorService=Executors.newFixedThreadPool(10);final  int PRECISION=100000;//定義各個引用變量/ 1000000000.0BigDecimal PI = null;//PIint i=0;   long startTime = System.currentTimeMillis();//開始時間System.out.println("計算機正在全力計算中...請稍候..."+startTime);while (true){if(System.currentTimeMillis()-startTime>180000) break;CountDownLatch countDownLatch=new CountDownLatch(10);for(int j=0;j<10;j++){MutiPi cur=new MutiPi(i++,countDownLatch);executorService.execute(cur);}try { System.out.println(tempt);countDownLatch.await();} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("wcl");System.out.println(tempt);PI = tempt.divide(BigDecimal.valueOf(64));//PI=tempt/64long endTime = System.currentTimeMillis();//結束時間System.out.println(PI.toString());Lifegame.writeTxt(PI.toString());executorService.shutdown();}
}
public class MutiPi implements Runnable {int cur;final  int PRECISION=100000;CountDownLatch countDownLatch;Lock lock=new ReentrantLock();BigDecimal bigDecimal=BigDecimal.valueOf(0);public MutiPi(int cur, CountDownLatch countDownLatch) {this.cur = cur;this.countDownLatch=countDownLatch;}@Overridepublic void run() {BigDecimal a = null;//公式的第一項BigDecimal b = null;//公式的第二項BigDecimal c = null;//公式的第三項BigDecimal d = null;//公式的第四項BigDecimal e = null;//公式的第五項BigDecimal f = null;//公式的第六項BigDecimal g = null;//公式的第七項BigDecimal h = null;//公式的第八項//a=(-1/1024)^ia = BigDecimal.valueOf(-1).divide(BigDecimal.valueOf(1024),PRECISION, BigDecimal.ROUND_DOWN).pow(cur);//valueOf()方法,BigDecimal的靜態方法,將 double 轉換為 BigDecimal//pow()方法,BigDecimal類中的方法,原型BigDecimal pow(int n, MathContext mc);//返回其值為 (this^n) 的 BigDecimal//b=32/(4i+1)b = BigDecimal.valueOf(32).divide(BigDecimal.valueOf(4).multiply(BigDecimal.valueOf(cur)).add(BigDecimal.valueOf(1)),PRECISION, BigDecimal.ROUND_DOWN);//為求精確,計算精度比輸出值多三位//ROUND_DOWN接近零的舍入模式(截取)//c=1/(4n+3)c = BigDecimal.valueOf(1).divide(BigDecimal.valueOf(4).multiply(BigDecimal.valueOf(cur)).add(BigDecimal.valueOf(3)),PRECISION, BigDecimal.ROUND_DOWN);//d=256/(10n+1)d = BigDecimal.valueOf(256).divide(BigDecimal.valueOf(10).multiply(BigDecimal.valueOf(cur)).add(BigDecimal.valueOf(1)),PRECISION, BigDecimal.ROUND_DOWN);//e=64/(10n+3)e = BigDecimal.valueOf(64).divide(BigDecimal.valueOf(10).multiply(BigDecimal.valueOf(cur)).add(BigDecimal.valueOf(3)),PRECISION, BigDecimal.ROUND_DOWN);//f=4/(10n+5)f = BigDecimal.valueOf(4).divide(BigDecimal.valueOf(10).multiply(BigDecimal.valueOf(cur)).add(BigDecimal.valueOf(5)),PRECISION, BigDecimal.ROUND_DOWN);//g=4/(10n+7)g = BigDecimal.valueOf(4).divide(BigDecimal.valueOf(10).multiply(BigDecimal.valueOf(cur)).add(BigDecimal.valueOf(7)),PRECISION, BigDecimal.ROUND_DOWN);//h=1/(10n+9)h = BigDecimal.valueOf(1).divide(BigDecimal.valueOf(10).multiply(BigDecimal.valueOf(cur)).add(BigDecimal.valueOf(9)),PRECISION, BigDecimal.ROUND_DOWN);CalculatePThread.add (a.multiply(d.add(h).subtract(b).subtract(c).subtract(e).subtract(f).subtract(g)));System.out.println("線程"+cur+"計算完成"+countDownLatch.getCount());countDownLatch.countDown();}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392191.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392191.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392191.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

linux復制文件跳過相同,Linux cp指令,怎么跳過相同的文件

1、使用cp命令的-n參數即可跳過相同的文件 。2、cp命令使用詳解&#xff1a;1)、用法&#xff1a;cp [選項]... [-T] 源文件 目標文件或&#xff1a;cp [選項]... 源文件... 目錄或&#xff1a;cp [選項]... -t 目錄 源文件...將源文件復制至目標文件&#xff0c;或將多個源文件…

eclipse類自動生成注釋

1.創建新類時自動生成注釋 window&#xff0d;>preference&#xff0d;>java&#xff0d;>code styple&#xff0d;>code template 當你選擇到這部的時候就會看見右側有一個框顯示出code這個選項&#xff0c;你點開這個選項&#xff0c;點一下他下面的New …

rman恢復

--建表create table sales( product_id number(10), sales_date date, sales_cost number(10,2), status varchar2(20));--插數據insert into sales values (1,sysdate-90,18.23,inactive);commit; --啟用rman做全庫備份 運行D:\autobackup\rman\backup_orcl.bat 生成…

微軟大數據_我對Microsoft的數據科學采訪

微軟大數據Microsoft was one of the software companies that come to hire interns at my university for 2021 summers. This year, it was the first time that Microsoft offered any Data Science Internship for pre-final year undergraduate students.微軟是到2021年夏…

再次檢查打印機名稱 并確保_我們的公司名稱糟透了。 這是確保您沒有的方法。...

再次檢查打印機名稱 并確保by Dawid Cedrych通過戴維德塞德里奇 我們的公司名稱糟透了。 這是確保您沒有的方法。 (Our company name sucked. Here’s how to make sure yours doesn’t.) It is harder than one might think to find a good business name. Paul Graham of Y …

linux中文本查找命令,Linux常用的文本查找命令 find

一、常用的文本查找命令grep、egrep命令grep&#xff1a;文本搜索工具&#xff0c;根據用戶指定的文本模式對目標文件進行逐行搜索&#xff0c;先是能夠被模式匹配到的行。后面跟正則表達式&#xff0c;讓grep工具相當強大。-E之后還支持擴展的正則表達式。# grep [options] …

分布與并行計算—日志挖掘(Java)

日志挖掘——處理數據、計費統計 1、讀取附件中日志的內容&#xff0c;找出自己學號停車場中對應的進出車次數&#xff08;in/out配對的記錄數&#xff0c;1條in、1條out&#xff0c;視為一個車次&#xff0c;本日志中in/out為一一對應&#xff0c;不存在缺失某條進或出記錄&a…

《人人都該買保險》讀書筆記

內容目錄&#xff1a; 1.你必須知道的保險知識 2.家庭理財的必需品 3.保障型保險產品 4.儲蓄型保險產品 5.投資型保險產品 6.明明白白買保險 現在我所在的公司Manulife是一家金融保險公司&#xff0c;主打業務就是保險&#xff0c;因此我需要熟悉一下保險的基礎知識&#xff0c…

Linux下查看txt文檔

當我們在使用Window操作系統的時候&#xff0c;可能使用最多的文本格式就是txt了&#xff0c;可是當我們將Window平臺下的txt文本文檔復制到Linux平臺下查看時&#xff0c;發現原來的中文所有變成了亂碼。沒錯&#xff0c; 引起這個結果的原因就是兩個平臺下&#xff0c;編輯器…

如何擊敗騰訊_擊敗股市

如何擊敗騰訊個人項目 (Personal Proyects) Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an…

滑塊 組件_組件制作:如何使用鏈接的輸入創建滑塊

滑塊 組件by Robin Sandborg羅賓桑德伯格(Robin Sandborg) 組件制作&#xff1a;如何使用鏈接的輸入創建滑塊 (Component crafting: how to create a slider with a linked input) Here at Stacc, we’re huge fans of React and the render-props pattern. When it came time…

配置靜態IPV6 NAT-PT

一.概述&#xff1a; IPV6 NAT-PT( Network Address Translation - Port Translation)應用與ipv4和ipv6網絡互訪的情況&#xff0c;根據參考鏈接配置時出現一些問題&#xff0c;所以記錄下來。參考鏈接&#xff1a;http://www.cisco.com/en/US/tech/tk648/tk361/technologies_c…

linux 線程與進程 pid,linux下線程所屬進程號問題

這一段看《unix環境高級編程》&#xff0c;一個關于線程的小例子。#include#include#includepthread_t ntid;void printids(const char *s){pid_t pid;pthread_t tid;pidgetpid();tidpthread_self();printf("%s pid %u tid %u (0x%x)n",s,(unsigned int)pid,(unsigne…

python3虛擬環境中解決 ModuleNotFoundError: No module named '_ssl'

前提是已經安裝了openssl 問題 當我在python3虛擬環境中導入ssl模塊時報錯&#xff0c;報錯如下&#xff1a; (py3) [rootlocalhost Python-3.6.3]# python3 Python 3.6.3 (default, Nov 19 2018, 14:18:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux Type "help…

python 使用c模塊_您可能沒有使用(但應該使用)的很棒的Python模塊

python 使用c模塊by Adam Goldschmidt亞當戈德施密特(Adam Goldschmidt) 您可能沒有使用(但應該使用)的很棒的Python模塊 (Awesome Python modules you probably aren’t using (but should be)) Python is a beautiful language, and it contains many built-in modules that…

分布與并行計算—生產者消費者模型實現(Java)

在實際的軟件開發過程中&#xff0c;經常會碰到如下場景&#xff1a;某個模塊負責產生數據&#xff0c;這些數據由另一個模塊來負責處理&#xff08;此處的模塊是廣義的&#xff0c;可以是類、函數、線程、進程等&#xff09;。產生數據的模塊&#xff0c;就形象地稱為生產者&a…

通過Xshell登錄遠程服務器實時查看log日志

主要想總結以下幾點&#xff1a; 1.如何使用生成密鑰的方式來登錄Xshell連接遠端服務器 2.在遠程服務器上如何上傳和下載文件&#xff08;下載log文件到本地&#xff09; 3.如何實時查看log&#xff0c;提取錯誤信息 一. 使用生成密鑰的方式來登錄Xshell連接遠端服務器 ssh登錄…

如何將Jupyter Notebook連接到遠程Spark集群并每天運行Spark作業?

As a data scientist, you are developing notebooks that process large data that does not fit in your laptop using Spark. What would you do? This is not a trivial problem.作為數據科學家&#xff0c;您正在開發使用Spark處理筆記本電腦無法容納的大數據的筆記本電腦…

是銀彈嗎?業務基線方法論

Fred.Brooks在1987年就提出&#xff1a;沒有銀彈。沒有任何一項技術或方法可以能讓軟件工程的生產力在十年內提高十倍。 我無意挑戰這個理論&#xff0c;只想討論一個方案&#xff0c;一個可能大幅提高業務系統開發效率的方案。 方案描述 我管這個方案叫做“由基線擴展…

linux core無權限,linux – 為什么編輯core_pattern受限制?

當我試圖為故意崩潰的程序生成核心文件時,最初的核心文件生成似乎被abrt-ccpp阻礙了.所以我嘗試用vim手動編輯/ proc / sys / kernel / core_pattern&#xff1a;> sudo vim /proc/sys/kernel/core_pattern當我試圖保存文件時,vim報告了這個錯誤&#xff1a;"/proc/sys…