logo资料库

CD租售多线程实验报告.docx

第1页 / 共11页
第2页 / 共11页
第3页 / 共11页
第4页 / 共11页
第5页 / 共11页
第6页 / 共11页
第7页 / 共11页
第8页 / 共11页
资料共11页,剩余部分请下载后查看
CD 租售多线程实验报告 一、实验目的 1:线程的创建和启动; 2:掌握线程运行的状态; 3:合理分配等待与睡眠的时间; 4:了解线程的同步,并重写 run 方法; 5:实现输入输出流文件写入; 二、实验步骤 1:建立 SaleCD 与 RentCD 的类,其中包括销售的种类及每种的数目,以及租赁的数 目,还包括两个类的构造函数 2:构建 CDShop 类,同时初始化销售租赁的 vector 类数组,定义租售方法,同时开 启销售,租赁,补货三个线程。 3:main 函数中实现多线程,并在两分钟后暂停运行。 三、实验源代码 package 多线程; import java.util.Date; import java.util.Random; import java.util.Vector; import java.io.FileWriter; import java.io.IOException; class SaleCD { String Name; int count; SaleCD(String Name, int count) { this.Name=Name; this.count=count;} } class RentCD
String Name; int ID; int count; boolean isRent=false; RentCD(String Name, int id,int count) { this.Name=Name; this.ID=id; this.count=count; } { } public class CDShop { Vector vRCD=new Vector(); Vector vSCD=new Vector(); FileWriter f; CDShop(FileWriter f) { for (int i=1;i<11;i++) { vRCD.add(new RentCD("RentCD"+i,i,10)); vSCD.add(new SaleCD("SaleCD"+i,10)); } this.f=f; } synchronized void rentCD() { Random r=new Random(); int i=r.nextInt(1); RentCD rcd=vRCD.get(i); int num= r.nextInt(8)+2; System.out.println(new Date()+"尝试租借" +" "+num+"张"+ ""); try { } f.write(new Date()+"尝试租借" +" "+num+"张"+"\r\n");
catch (IOException e) { e.printStackTrace(); } { while (num>rcd.count) { System.out.println(new Date()+"租借" +"已借出"); try { f.write(new Date()+"租借" +"已借出"+"\r\n"); } catch (IOException e) { e.printStackTrace(); } if (r.nextBoolean()==false) { System.out.println(new Date()+"租借" +"不成功于是选择放弃"); try { f.write(new Date()+"租借" +"不成功于是选择放弃"+"\r\n"); } catch (IOException e) { e.printStackTrace(); } return; } } rcd.isRent=true; System.out.println(new Date()+"成功租借" + " "+num+"张"); try { f.write(new Date()+"成功租借" +" "+ num+"张"+"\r\n"); } catch (IOException e) { e.printStackTrace(); } rcd.count=rcd.count-num;
try { wait(200+r.nextInt(100)); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } rcd.isRent=false; System.out.println(new Date()+"归还" +" "+(10-rcd.count)+"张"); try { f.write(new Date()+"归还" +" "+(10-rcd.count)+"张"+"\r\n"); } catch (IOException e) { e.printStackTrace(); } rcd.count=10; } } synchronized void saleCD() { Random rand=new Random(); int i= rand.nextInt(10); SaleCD scd=vSCD.get(i); int num= rand.nextInt(8)+2; System.out.println(new Date()+"尝试购买"+scd.Name+" "+num+"张"); try { f.write(new Date()+"尝试购买"+scd.Name+" "+num+"张"+"\r\n"); } catch (IOException e) { e.printStackTrace(); } while(num>scd.count) { System.out.println( new Date()+scd.Name+"缺货"); try { f.write( new Date()+scd.Name+"缺货"+"\r\n"); }
catch (IOException e) { e.printStackTrace(); } if(rand.nextBoolean()==true) { if(rand.nextBoolean()==true) { System.out.println(new Date()+"由于很想购买"+scd.Name+"于是紧 急启动进货行动"); try { 货行动"+"\r\n"); f.write(new Date()+"由于很想购买"+scd.Name+"于是紧急启动进 } catch (IOException e) { e.printStackTrace(); } new Order_requestTrd(this,i,f); } else { try { System.out.println(new Date()+scd.Name+"缺货"+"选择等待 "); "+"\r\n"); try { f.write(new Date()+scd.Name+"缺货"+"选择等待 } catch (IOException e) { e.printStackTrace(); } wait(200); } catch (InterruptedException e) { e.printStackTrace();
} } } if(rand.nextBoolean()==false) { System.out.println( new Date()+scd.Name+"缺货"+"选择放弃"); try { f.write(new Date()+scd.Name+"缺货"+"选择放弃"+"\r\n"); } catch (IOException e) { e.printStackTrace(); } } } System.out.println( new Date()+"成功购买"+scd.Name+" "+num+"张"); try { f.write( new Date()+"成功购买"+scd.Name+" "+num+"张"+"\r\n"); } catch (IOException e) { e.printStackTrace(); } scd.count=scd.count-num; } public static void main(String[] args) throws InterruptedException { FileWriter f=null; try { f=new FileWriter("record.txt"); } catch (IOException e) { e.printStackTrace(); }
CDShop c=new CDShop(f); RControlTrd rt=new RControlTrd(c); SControlTrd st=new SControlTrd(c); OrderTrd ot=new OrderTrd(c,f); Thread.sleep(120*1000); } } class RentTrd extends Thread { CDShop c ; RentTrd(CDShop c) { this.c=c; this.setDaemon(true); this.start(); } public void run() { c.rentCD(); } } class SaleTrd extends Thread { CDShop c; SaleTrd(CDShop c) { this.c=c; this.setDaemon(true); this.start(); } public void run() { c.saleCD(); } } class Order_requestTrd extends Thread
{ CDShop c; int i; FileWriter f; Order_requestTrd(CDShop c,int i,FileWriter f) { this.c=c; this.i=i; this.f=f; this.setDaemon(true); this.start(); } public void run() { System.out.println( new Date()+"紧急补充"+c.vSCD.get(i).Name+" "+(10-c.vSCD.get(i).count)+"张"); try { f.write( new Date()+"紧急补充"+c.vSCD.get(i).Name+" "+(10-c.vSCD.get(i).count)+"张"+"\r\n"); } catch (IOException e) { e.printStackTrace(); } c.vSCD.get(i).count=10; } } class OrderTrd extends Thread { CDShop c; FileWriter f; OrderTrd(CDShop c,FileWriter f) { this.c=c; this.f=f; this.setDaemon(true); this.start(); } public void run()
分享到:
收藏