多线程顺序查找算法实现_第1页
多线程顺序查找算法实现_第2页
多线程顺序查找算法实现_第3页
多线程顺序查找算法实现_第4页
多线程顺序查找算法实现_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选优质文档-倾情为你奉上J2EE程序设计2017年春作业第一次作业(5分)多线程并发查找:Math类中的随机函数产生10000个随机数并存于数组。从键盘接收一个数x,然后用多线程并发查找x在数组中的所有下标位置。 查找线程的构造函数形参如下所示: 数组名, 查找范围(起始下标、结束下标) 每个线程体在查找范围内顺序查找,并将该范围内所有找到的x的下标记录到共享的一个内存缓冲区。创建上述线程类的四个实体对象,用四个线程将数组分成不重叠的4段进行查找,如果查找失败,则内存缓冲区为空,输出相应信息;否则顺序输出缓冲区中的所有下标位置(注意:这些下标位置并不是由小到大顺序排列的)。输出结果:(上面利

2、用缓冲区做的,下面是通过管道的输出)代码: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通过管道实现 4个发送线程,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();专心-专注-专业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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论