多线程顺序查找算法实现(共6页).docx

上传人:飞****2 文档编号:13838520 上传时间:2022-05-01 格式:DOCX 页数:6 大小:46.44KB
返回 下载 相关 举报
多线程顺序查找算法实现(共6页).docx_第1页
第1页 / 共6页
多线程顺序查找算法实现(共6页).docx_第2页
第2页 / 共6页
点击查看更多>>
资源描述

《多线程顺序查找算法实现(共6页).docx》由会员分享,可在线阅读,更多相关《多线程顺序查找算法实现(共6页).docx(6页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

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;i10000;i+)iii = (int)(Math.random()*1000);return ii;public static void main(String arg) t

3、hrows 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(ii,2000,3999); t3

4、.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.println(n通过管道实现 4个发送线程

5、,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);for(int i = 0 ;i n;i+)sei

6、= new Send(ii,i*2000,i*2000+1999);sei.setX(x); sei.setBuf(bf);sei.start();for(int i = 0;im;i+)rei.start();专心-专注-专业public class BUF private int indexs = new int10000; private int index = -1;private boolean isPutting = false;public BUF() super();/ TODO Auto-generated constructor stubpublic BUF(int ind

7、exs, 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 = -1)System.out.println(没有找到);else for(i

8、nt 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,int x,BUF bf) super();this.indexs = indexs;this.start = s;this.en

9、d = 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.start ;ithis.end;i+)if(this.indexsi = this.x)bf.putIndex(i);/Syste

10、m.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;public void setNo(int no)this.no = no;b = new byte4;public Send(int indexs,int s,

11、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();this.indexs = indexs;this.start = s;this.end = e;b = new byte4;public void setX(i

12、nt x)this.x = x;public void setBuf(BUF bf)this.bf = bf;public void run()try for(int i = this.start ;i1) count-;else out.close(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();static PipedOutputStream out;static int count = 0;public static Send init(PipedOutputStream pos

13、, int n)Send se = new Sendn;count = n;out = pos;for(int i = 0;ise.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(Exception e)static PipedInputStream in;static int count = 0;public static Receive init(PipedInputStream pis , int m) Receive re = new Receivem;count = m;in = pis;for(int i = 0; i re.length;i+)rei = new Receive();return re;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁