多线程顺序查找算法实现

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

J2EE程序设计2017年春作业第一次作业(5分)多线程并发查找:Math类中的随机函数产生10000个随机数并存于数组。从键盘接收一个数x,然后用多线程并发查找x在数组中的所有下标位置。查找线程的构造函数形参如下所示:数组名,查找范围(起始下标、结束下标)每个线程体在查找范围内顺序查找,并将该范围内所有找到的x的下标记录到共享的一个内存缓冲区。创建上述线程类的四个实体对象,用四个线程将数组分成不重叠的4段进行查找,如果查找失败,则内存缓冲区为空,输出相应信息;否则顺序输出缓冲区中的所有下标位置(注意:这些下标位置并不是由小到大顺序排列的)。输出结果:(上面利用缓冲区做的,下面是通过管道的输出)代码:importjava.io.IOException;importjava.io.PipedInputStream;importjava.io.PipedOutputStream;importjava.util.Scanner;publicclassWork1{publicint[]getInts(){int[]ii=newint[10000];for(inti=0;i10000;i++){ii[i]=(int)(Math.random()*1000);}returnii;}publicstaticvoidmain(String[]arg)throwsInterruptedException{Work1wk=newWork1();int[]ii=wk.getInts();intx=0;System.out.println(请输入X的值:);Scannerscanner=newScanner(System.in);x=scanner.nextInt();BUFbf=newBUF();MyThreadt1=newMyThread(ii,0,1999);t1.setX(x);t1.setBuf(bf);MyThreadt3=newMyThread(ii,2000,3999);t3.setX(x);t3.setBuf(bf);MyThreadt2=newMyThread(ii,4000,5999);t2.setX(x);t2.setBuf(bf);MyThreadt4=newMyThread(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.println(\n通过管道实现4个发送线程,2个接受线程);PipedOutputStreamout=newPipedOutputStream();PipedInputStreamin=newPipedInputStream();try{out.connect(in);}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}intn=4,m=2;Sendse[]=Send.init(out,n);Receivere[]=Receive.init(in,m);for(inti=0;in;i++){se[i]=newSend(ii,i*2000,i*2000+1999);se[i].setX(x);se[i].setBuf(bf);se[i].start();}for(inti=0;im;i++){re[i].start();}}}publicclassBUF{privateint[]indexs=newint[10000];privateintindex=-1;privatebooleanisPutting=false;publicBUF(){super();//TODOAuto-generatedconstructorstub}publicBUF(int[]indexs,intindex){super();this.indexs=indexs;this.index=index;}synchronizedpublicvoidputIndex(intind){if(isPutting){try{wait();}catch(InterruptedExceptione){}}isPutting=true;this.indexs[++index]=ind;isPutting=false;notify();}publicvoidprintIndexs(){if(index==-1){System.out.println(没有找到);}else{for(inti=0;i=this.index;i++){System.out.print(indexs[i]+\t);}}}}packagework;publicclassMyThreadextendsThread{privateBUFbf=newBUF();privateint[]indexs;privateintstart;privateintend;privateintx;publicMyThread(int[]indexs,ints,inte,intx,BUFbf){super();this.indexs=indexs;this.start=s;this.end=e;this.x=x;this.bf=bf;}publicMyThread(int[]indexs,ints,inte){super();this.indexs=indexs;this.start=s;this.end=e;}publicvoidsetX(intx){this.x=x;}publicvoidsetBuf(BUFbf){this.bf=bf;}publicvoidrun(){for(inti=this.start;ithis.end;i++){if(this.indexs[i]==this.x){bf.putIndex(i);//System.out.println(OK);}}}}importjava.io.IOException;importjava.io.PipedOutputStream;publicclassSendextendsThread{privateBUFbf=newBUF();privateint[]indexs;privateintstart;privateintend;privateintx;intno;byte[]b;publicvoidsetNo(intno){this.no=no;b=newbyte[4];}publicSend(int[]indexs,ints,inte,intx,BUFbf){super();this.indexs=indexs;this.start=s;this.end=e;this.x=x;this.bf=bf;b=newbyte[4];}publicSend(){super();//TODOAuto-generatedconstructorstub}publicSend(int[]indexs,ints,inte){super();this.indexs=indexs;this.start=s;this.end=e;b=newbyte[4];}publicvoidsetX(intx){this.x=x;}publicvoidsetBuf(BUFbf){this.bf=bf;}publicvoidrun(){try{for(inti=this.start;ithis.end;i++){if(this.indexs[i]==this.x){bf.putIndex(i);//System.out.println(OK);Chg.toBA(i,b);out.write(b);try{sleep(1);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}if(count1){count--;}else{out.close();}}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}staticPipedOutputStreamout;staticintcount=0;publicstaticSend[]init(PipedOutputStreampos,intn){Sendse[]=newSend[n];count=n;out=pos;for(inti=0;ise.length;i++){se[i]=newSend();}returnse;}}importjava.io.PipedInputStream;publicclassReceiveextendsThread{byte[]b;publicReceive(){b=newbyte[4];}publicvoidrun(){try{while(true){if(in.read(b)==-1)break;intx=Chg.toInt(b);System.out.print(x+\t);}//in.close();}catch(Exceptione){}}staticPipedInputStreamin;staticintcount=0;publicstaticReceive[]init(PipedInputStreampis,intm){Receivere[]=newReceive[m];count=m;in=pis;for(inti=0;ire.length;i++){re[i]=newReceive();}returnre;}}

1 / 8
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功