多線程順序查找算法實現(xiàn)_第1頁
多線程順序查找算法實現(xiàn)_第2頁
多線程順序查找算法實現(xiàn)_第3頁
多線程順序查找算法實現(xiàn)_第4頁
多線程順序查找算法實現(xiàn)_第5頁
已閱讀5頁,還剩1頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上J2EE程序設(shè)計2017年春作業(yè)第一次作業(yè)(5分)多線程并發(fā)查找:Math類中的隨機函數(shù)產(chǎn)生10000個隨機數(shù)并存于數(shù)組。從鍵盤接收一個數(shù)x,然后用多線程并發(fā)查找x在數(shù)組中的所有下標位置。 查找線程的構(gòu)造函數(shù)形參如下所示: 數(shù)組名, 查找范圍(起始下標、結(jié)束下標) 每個線程體在查找范圍內(nèi)順序查找,并將該范圍內(nèi)所有找到的x的下標記錄到共享的一個內(nèi)存緩沖區(qū)。創(chuàng)建上述線程類的四個實體對象,用四個線程將數(shù)組分成不重疊的4段進行查找,如果查找失敗,則內(nèi)存緩沖區(qū)為空,輸出相應(yīng)信息;否則順序輸出緩沖區(qū)中的所有下標位置(注意:這些下標位置并不是由小到大順序排列的)。輸出結(jié)果:(上面利

2、用緩沖區(qū)做的,下面是通過管道的輸出)代碼:import java.io.IOException;import java.io.PipedInputStream;import java.io.PipedOutputStream;import java.util.Scanner;public class Work1 public int getInts()int ii = new int10000;for(int i=0;i<10000;i+)iii = (int)(Math.random()*1000);return ii;public static void main(String ar

3、g) throws InterruptedException Work1 wk = new Work1();int ii = wk.getInts();int x =0;System.out.println("請輸入X的值:");Scanner scanner = new Scanner(System.in);x = scanner.nextInt();BUF bf = new BUF();MyThread t1 = new MyThread(ii,0,1999); t1.setX(x); t1.setBuf(bf);MyThread t3 = new MyThread(i

4、i,2000,3999); t3.setX(x); t3.setBuf(bf);MyThread t2 = new MyThread(ii,4000,5999); t2.setX(x); t2.setBuf(bf);MyThread t4 = new MyThread(ii,6000,7999); t4.setX(x); t4.setBuf(bf);t1.start(); t2.start(); t3.start(); t4.start();t1.join(); t2.join(); t3.join(); t4.join();bf.printIndexs();System.out.printl

5、n("n通過管道實現(xiàn) 4個發(fā)送線程,2個接受線程");PipedOutputStream out = new PipedOutputStream();PipedInputStream in = new PipedInputStream();try out.connect(in); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();int n = 4 , m=2;Send se = Send.init(out,n);Receive re = Receive.init(in, m

6、);for(int i = 0 ;i < n;i+)sei = new Send(ii,i*2000,i*2000+1999);sei.setX(x); sei.setBuf(bf);sei.start();for(int i = 0;i<m;i+)rei.start();專心-專注-專業(yè)public class BUF private int indexs = new int10000; private int index = -1;private boolean isPutting = false;public BUF() super();/ TODO Auto-generat

7、ed constructor stubpublic BUF(int indexs, int index) super();this.indexs = indexs;this.index = index;synchronized public void putIndex(int ind)if(isPutting)trywait();catch(InterruptedException e)isPutting = true;this.indexs+index = ind;isPutting = false;notify();public void printIndexs()if(index = -

8、1)System.out.println("沒有找到");else for(int i = 0;i<=this.index;i+)System.out.print(indexsi+"t");package work;public class MyThread extends Threadprivate BUF bf = new BUF();private int indexs;private int start;private int end;private int x;public MyThread(int indexs,int s,int e,

9、int x,BUF bf) super();this.indexs = indexs;this.start = s;this.end = e;this.x = x;this.bf = bf;public MyThread(int indexs,int s,int e) super();this.indexs = indexs;this.start = s;this.end = e;public void setX(int x)this.x = x;public void setBuf(BUF bf)this.bf = bf;public void run()for(int i = this.s

10、tart ;i<this.end;i+)if(this.indexsi = this.x)bf.putIndex(i);/System.out.println("OK");import java.io.IOException;import java.io.PipedOutputStream;public class Send extends Threadprivate BUF bf = new BUF();private int indexs;private int start;private int end;private int x;int no; byte b;

11、public void setNo(int no)this.no = no;b = new byte4;public Send(int indexs,int s,int e,int x,BUF bf) super();this.indexs = indexs;this.start = s;this.end = e;this.x = x;this.bf = bf;b = new byte4;public Send() super();/ TODO Auto-generated constructor stubpublic Send(int indexs,int s,int e) super();

12、this.indexs = indexs;this.start = s;this.end = e;b = new byte4;public void setX(int x)this.x = x;public void setBuf(BUF bf)this.bf = bf;public void run()try for(int i = this.start ;i<this.end;i+)if(this.indexsi = this.x)bf.putIndex(i);/System.out.println("OK");Chg.toBA(i, b);out.write(b

13、);try sleep(1); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();if(count>1) count-;else out.close(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();static PipedOutputStream out;static int count = 0;public static Send init(PipedOutput

14、Stream pos , int n)Send se = new Sendn;count = n;out = pos;for(int i = 0;i<se.length;i+)sei = new Send();return se;import java.io.PipedInputStream;public class Receive extends Threadbyte b;public Receive() b = new byte4;public void run()trywhile(true)if(in.read(b) = -1)break;int x = Chg.toInt(b);System.out.print(x+"t");/in.close();catch(Exce

溫馨提示

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

評論

0/150

提交評論