版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年福建省福鼎市高三数学下册期末考试模拟检测卷(模拟题)附答案
- 2026年湖南省耒阳市高三数学下册期末考试模拟考试卷附完整答案(必刷)
- 2026年海南省五指山市高三数学下册期末考试模拟考试卷附参考答案【综合题】
- 医院放射科防护操作规范手册
- 产品试用计划实施通知函(5篇范文)
- 催办付款单据提交延误函(3篇)
- 关于贵方延迟交货的纠正函(3篇范文)
- 40分写作规范练(七)
- 医疗卫生手术室护士手术操作及护理质量绩效衡量表
- 文艺演出活动安全防护方案
- 2026年幼儿园保育员技能大赛理论试题(附答案)
- 高中体育与健康人教版必修第一册常见运动损伤预防与处理教学设计
- 临床实践中常见的伦理问题与应对
- 2026年卫生高级职称面审答辩(药学)副高经典试题及答案
- GB/T 1345-2026水泥细度检验方法筛析法
- 化工现场定置管理制度
- 五年级科学家长会教学课件
- 医院vi 设计合同标准文本
- 化工厂变换岗位考试题库
- 哲学类论文开题报告模板
- 2024建设工程施工合同(示范文本GF)
评论
0/150
提交评论