網(wǎng)絡編程多線程實驗報告_第1頁
網(wǎng)絡編程多線程實驗報告_第2頁
網(wǎng)絡編程多線程實驗報告_第3頁
網(wǎng)絡編程多線程實驗報告_第4頁
網(wǎng)絡編程多線程實驗報告_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、精品文檔XXX 大學(計算機學院)網(wǎng)絡編程技術課內實驗報告實驗名稱: 第一、二次實驗合:多線程實踐專業(yè)名稱:班 級:學生姓名:學號(8位)-指導教師:實驗日期:i歡在下載精品文檔. 實驗目的及實驗環(huán)境1 理解線程的定義和概念2 掌握線程的實現(xiàn)方案3 掌握線程的狀態(tài)4 掌握加速比的求解二 . 實驗內容實驗一:1對隨機產生的1M-10M的數(shù)組進行求和,根據(jù)實驗一要求采用多線程共同完 成任務。2 使用蒙特卡洛算法求PI的值,實驗統(tǒng)計的總次數(shù)不得低于10M次,根據(jù)實驗一要求采用多線程完成PI 的求解;3 下載四大名著之一紅樓夢的文本全文,統(tǒng)計全文中“賈寶玉”和“林黛玉”的出現(xiàn)的次數(shù);根據(jù)實驗一要求采用

2、多線程完成上述任務。實驗一要求: 上述三個問題,分別用串行, 1 道線程, 2 道線程, 4 道線程, 8道線程來完成, 并統(tǒng)計完成實驗同一樣本所使用的時間, 做出統(tǒng)計圖。 如果多線程的采用降低了求解的效率, 是否存在為共享問題, 請給出解決方案。 并根據(jù)加速比的計算公式,計算并行化之后的加速比。(加速比=串行時間/ 并行時間) 實驗 2:4 使用二分查查使用多線程進行求解。查找的有序數(shù)據(jù)量不少于1M。5 對歸并排序使用多線程進行求解。排序數(shù)據(jù)量不少于1M。6 對快速排序使用多線程進行求解。排序數(shù)據(jù)量不少于1M。二.萬案設計1. 數(shù)組求和設計:將大數(shù)組根據(jù)線程的數(shù)量進行拆分。:每個線程對數(shù)組的

3、部分元素進行求和。:對剩余數(shù)組求和:等待所有線程執(zhí)行完畢,累加和,返回結果。2. 蒙特卡洛求PI : 開啟多個線程分別用蒙特卡洛算法求解PI : 計算結果累加除以開啟線程個數(shù)( 開啟線程個數(shù)越多,結果越精確 )3. 二分查找使用多線程:將給定1M數(shù)據(jù)量數(shù)組排序:將數(shù)組劃分成多塊:開啟相對線程個數(shù):每個線程對自己內部數(shù)組進行二分查找:找到返回位置,沒找到等待線程全部結束4. 多線程歸并排序:將給定數(shù)組進行劃分:創(chuàng)建線程執(zhí)行歸并排序:進程同步:返回有序數(shù)組5. 多線程快速排序:將需要排序的數(shù)組分成多組:對每組實現(xiàn)一個線程來快速排序:最后用歸并排序算法的思想合并這些數(shù)組:最終實現(xiàn)原序列的排序過程四.

4、測試數(shù)據(jù)及運行結果1 .多線程數(shù)組求cterminated? ThreadsSum tJava Appliestion CAProgram FilesMi thr'ead count: 15多線程并度求和需要時間 9 多線程并發(fā)求箝5235了圮20 單線程并發(fā)求和需要時間;5 單線程基*求和.5235733202 .蒙特卡洛求PI<tprrninat?ri > PI Runnahle Java Appl caTicnJ C:Prngrdm IFil?sj£PI(n)=,1口 1597最 為 5Mftjnnrible求P1(n)并行時間 t 712PI(n) =對上

5、也B 男。42g求PlM)串行時間212/10 相對疝途比:1740/7154 .多線程二分查找terminated> rhedd Bi nary SearchAppli<dtion C;PrQgram Fi查找闈是56A找到位置為,59川15 .多線程歸并排序terminated =* McrgeSort Java Application CAProgram FilesJava' 歸并排序時間五6毫秒6 .多線程快速排序<rfirminriTer|? Fasts art Java Applkcit ion CAFro gram FileiMvfljd 務鏤程快邨察曹

6、時鬧+44 7五.總結多線程并發(fā)實驗中,解決了之前學過的一些算法,fork操作可以將一個大的問題劃分為若干個較小的問題,從而在遞歸的過程中,到直接進行計算的粒度 適合的子問題;子問題在結算后,可以得到整個問題的部分解join操作收集子結果,合并,得到完整解。這座方式對遞歸的應用更加靈活廣泛。并且次方發(fā)法 減少等待時間,此外提高性能。而之前都是用行解決,多線程并發(fā)處理會提升性 能,為了讓程序運行的更快。但是,并不是啟動更多的線程能讓程序最大限度的 并發(fā)執(zhí)行。有些任務,資源越多,那么就越完成的快。但是有些任務本質上是申 行的,即使增加再多的資源也未必能夠提高速度。通過分治算法算法來解決問題, 為此

7、我借閱相關書籍了解更多內容。4歡在下載精品文檔六 附錄: 源代碼(電子版, 紙質版不打印)1.import java.util.Random;public class TS public static long Tsum( final int array) if (array = null | array.length = 0) throw new IllegalArgumentException("array length must greater than 0"); final RuntimeData rd = new RuntimeData();int threadC

8、ount = rd.getThreadCount(array);System. out.println("thread count:" + threadCount);final int lenPerThread = array.length / threadCount;long s1=System. currentTimeMillis ();for ( int i = 0; i < threadCount; i+) final int index = i;new Thread() Overridepublic void run() long s = 0;int sta

9、rt = index * lenPerThread;int end = start + lenPerThread;for ( int j = start; j < end; j+) s += arrayj;synchronized (rd) rd.sum += s;rd.finishThreadCount+;.start();int remain = array.length % threadCount;long s = 0;for ( int i = array.length - remain; i < array.length; i+) s += arrayi;synchron

10、ized (rd) rd.sum += s;while (rd.finishThreadCount != threadCount) try Thread.sleep (1); catch (InterruptedException e) e.printStackTrace();break ;long s2=System. currentTimeMillis ()-s1;System. out.println(" 多線程并發(fā)求和需要時間: "+s2);return rd.sum;public static void main(String args) int arr= new

11、 int 1024*1024;Random r=new Random();for ( int i=0;i<arr.length;i+)arri=r.nextInt(1000);System. out .println("多線程并發(fā)求和:"+Tsum(arr);System. out .println("單線程基本求和:"+BasicCaculator.sum(arr);class RuntimeData long sum;int defThreadCount = 15;int finishThreadCount;public int getThre

12、adCount( int array) if (array.length < defThreadCount) return array.length; return defThreadCount;class BasicCaculator public static long sum( int numbers)long sum = 0;long s1=System. currentTimeMillis ();for ( int i=0;i<numbers.length;i+) sum += numbersi;long s2=System. currentTimeMillis ()-s

13、1;System. out.println(" 單線程并發(fā)求和需要時間: "+s2); return sum; 5. package 網(wǎng)絡編程實驗;import java.util.Random;import java.util.concurrent.CountDownLatch;public class MergeSort public static void main(String args) throws InterruptedExceptionint length = 1024*1024;int array = new intlength;Random random

14、 = new Random();for(int i = 0; i < length; i+)int x = random.nextInt(); arrayi = x; long time1 = System.currentTimeMillis();int minLength = length/2;int a = new intminLength;int b = new intminLength;for(int i=0; i<2; i+)int start = minLength * i;int end = minLength * (i + 1);if(i=0)for(int j=s

15、tart, k=0; j<end; j+,k+) ak = arrayj;else if(i=1)for(int j=start, k=0; j<end; j+,k+)bk = arrayj;CountDownLatch latch = new CountDownLatch(2);new Thread(new Runnable()Overridepublic void run() MergeSort.sort(a);latch.countDown();).start();new Thread(new Runnable()Overridepublic void run() Merge

16、Sort.sort(b);latch.countDown();).start();latch.await();merge(a, b, array);long time2 = System.currentTimeMillis();System.out.println("歸并排序時間: " + (time2 - time1) + " 毫秒 ");private static void merge(int a1, int a2, int tmpArray)int length1 = a1.length;int length2 = a2.length;int l

17、eft = 0;int right = 0;int pos = 0;while(left < length1 && right < length2)if(a1left <= a2right)tmpArraypos = a1left;left+;elsetmpArraypos = a2right;right+;pos+;while(left < length1)tmpArray pos+ = a1 left+ ;while(right < length2)tmpArray pos+ = a2 right+ ;/ 遞歸分解public static v

18、oid sort(int array)int length = array.length;int tmpArray = new int length ;sort(array, tmpArray, 0, length-1);public static void sort(int array, int tmpArray, int left, int right)if(left < right)int center = (left + right)/2;sort(array, tmpArray, left, center);sort(array, tmpArray, center+1, rig

19、ht);merge(array, tmpArray, left, center+1, right);/ 合并排序private static void merge(int array, int tmpArray, int leftStart, int rightStart, int rightEnd) int leftEnd = rightStart - 1;int tmpPos = leftStart;int total = rightEnd - leftStart + 1;while(leftStart <= leftEnd && rightStart <= r

20、ightEnd)if(array leftStart <= array rightStart )9歡迎下載 。精品文檔tmpArray tmpPos+ = array leftStart+ ;elsetmpArray tmpPos+ = array rightStart+ ; while(leftStart <= leftEnd)tmpArray tmpPos+ = array leftStart+ ;while(rightStart <= rightEnd)tmpArray tmpPos+ = array rightStart+ ;for(int i = 0; i <

21、 total; i+, rightEnd- )array rightEnd = tmpArray rightEnd ;import java.math.*;public class PI_Runnable throws InterruptedException public static void main(String口 args)/ TODOAuto-generated method stub12歡迎下載。/并行運算works works1 =works works2 =works works3 =Thread thread1 =Thread thread2 =Thread thread3

22、 = startTime = System. thread1.start();thread2.start();thread3.start();thread1.join();/ thread2.join();thread3.join(); endTime = System.long startTime ,endTime;double step = 1.0 / 100000000;newworks(1, 100000000);newworks(2, 100000000);newworks(3, 100000000);new Thread(works1);new Thread(works2);new

23、 Thread(works3);currentTimeMillis ();/獲取并行計算的開始時的系統(tǒng)時間實現(xiàn)線程并行currentTimeMillis ();/獲取并行計算的結束時的系統(tǒng)時間long ParallelTime = endTime - startTime;System. out.println("PI( 無)="+ (works1.getSum() + works2.getSum() + works3.getSum() * step);/并行計算求的PI(無)值System. out.println("Runnable 求 PI(無)并行時間:&q

24、uot;+ ParallelTime);/ 串行運算 works works5 = newworks(1, 100000000);startTime = System. currentTimeMillis ();/ 獲取串行計算的開始時的系統(tǒng)時間double PI = works5.GetPI();endTime = System. currentTimeMillis ();/ 獲取串行計算的結束時的系統(tǒng)時間long SeriaTime = endTime - startTime;System. out.println("PI( 無)="+ PI * step);/串行計算求的 PI(無)值System. out.println(" 求

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論