版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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单职招生面试题及答案
- 半导体运输设备维保工作手册
- 临床诊疗流程与病历书写规范手册
- 宁夏非公有制企业融资供需情况及投资评估规划分析研究报告
- 茶叶贸易防伪溯源管理工作手册
- 立陶宛银行业行业产业现状供需分析及创新投资评估规划分析研究报告
- 2025届聊城市冠县三下数学期末联考模拟试题(含解析)
- 2026三上数学毫米分米千米游戏课件
- 2025届福建省三明市清流县三年级数学第二学期期中监测模拟试题(含解析)
- 2025届石家庄市桥东区数学四年级第二学期期中统考模拟试题(含解析)
- 危险化学品目录(2025版)(精简版)
- DB52∕T 1715.1-2023 电动汽车充电基础设施规范第1部分:规划
- 解读慢性阻塞性肺病(GOLD)指南(2026)更新要点课件
- 光伏电站运行维护规程
- THIS001-2022红外热电堆传感器
- 外墙水包砂质保合同范本
- 肢体创伤后水肿管理指南解读
- 招标代理业务管理规范与操作指南
- 旅游直播培训课件
- T/CAPA 3-2021毛发移植规范
- 急诊治疗过程中的医患沟通技巧
评论
0/150
提交评论