资源预览内容
第1页 / 共8页
第2页 / 共8页
第3页 / 共8页
第4页 / 共8页
第5页 / 共8页
第6页 / 共8页
第7页 / 共8页
第8页 / 共8页
亲,该文档总共8页全部预览完了,如果喜欢就下载吧!
资源描述
云南大学软件学院 实验报告课程: 设计模式 学期: 2012-2013学年 第一学期 任课教师: 陆歌浩 专业: 软件工程 学号: 20101120007 姓名: 孙金文 成绩: 实验4 单件模式一、实验目的1.熟悉单件模式 2.运用单件模式编制简单的java程序二、实验内容实验代码:package headfirst.singleton;import javax.swing.JOptionPane;public class Singleton public static void main(String args) throws Exceptionint input;input = Integer.parseInt(JOptionPane.showInputDialog(请选择运行的单件模式的方式(123) +n“1”代表同步方式n“2”代表急切方式n“3”代表双重加锁方式); long StartTime=System.currentTimeMillis(); if (input=1) test1 xian1=new test1(); xian1.start1(); test11 xian11=new test11(); xian11.start11(); else if(input=2) test2 xian2=new test2(); xian2.start2(); test22 xian22=new test22(); xian22.start22(); else if(input=3) test3 xian3=new test3(); xian3.start3(); test33 xian33=new test33(); xian33.start33(); long EndTime=System.currentTimeMillis(); long a=EndTime-StartTime; JOptionPane.showMessageDialog(null,执行时间为:+a+ms., n执行结果(孙金文20101120007), JOptionPane.PLAIN_MESSAGE);package headfirst.singleton;public class Chocolateboiler1private boolean empty;private boolean boiled;private static Chocolateboiler1 uniqueinstance;private Chocolateboiler1()empty=true;boiled=false;public static synchronized Chocolateboiler1 getinstance()if(uniqueinstance=null)uniqueinstance=new Chocolateboiler1();return uniqueinstance;public void fill()if(isempty()empty=false;boiled=false;public void drain()if(!isempty()&isboiled()empty=true;public void boil()if(!isempty() &!isboiled()boiled=true;public boolean isempty()return empty;public boolean isboiled()return boiled;package headfirst.singleton;public class Chocolateboiler2private boolean empty;private boolean boiled;private static Chocolateboiler2 uniqueinstance=new Chocolateboiler2();private Chocolateboiler2()empty=true;boiled=false;public static Chocolateboiler2 getinstance()return uniqueinstance;public void fill()if(isempty()empty=false;boiled=false;public void drain()if(!isempty()&isboiled()empty=true;public void boil()if(!isempty() &!isboiled()boiled=true;public boolean isempty()return empty;public boolean isboiled()return boiled;package headfirst.singleton;public class Chocolateboiler3private boolean empty;private boolean boiled;private volatile static Chocolateboiler3 uniqueinstance;private Chocolateboiler3()empty=true;boiled=false;public static Chocolateboiler3 getinstance()if(uniqueinstance=null)synchronized(Chocolateboiler3.class)if(uniqueinstance=null) uniqueinstance=new Chocolateboiler3();return uniqueinstance;public void fill()if(isempty()empty=false;boiled=false;public void drain()if(!isempty()&isboiled()empty=true;public void boil()if(!isempty() &!isboiled()boiled=true;public boolean isempty()return empty;public boolean isboiled()return boiled;package headfirst.singleton;public class test1 extends Thread public void start1()for(int i=0;i1000000;i+) Chocolateboiler1.getinstance();package headfirst.singleton;public class test11 extends Threadpublic void start11() for(int i=0;i2000000;i+) Chocolateboiler1.getinstance(); package headfirst.singleton;public class test2 extends Thread public void start2()for(int i=0;i1000000;i+) Chocolateboiler2.getinstance();package headfirst.singleton;public class test22 extends Threadpublic void start22() for(int i=0;i2000000;i+) Chocolateboiler2.getinstance(); package headfirst.singleton;public class test3 extends Thread public void start3()for(int i=0;i1000000;i+) Chocolateboiler3.getinstance();package headfirst.singleton;public class test33 extends Threadpublic void start33() for(int i=0;i双重加锁同步方式”。 / 文档可自由编辑打印
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号