云大设计模式实验4单件模式

上传人:壹****1 文档编号:488261876 上传时间:2022-11-20 格式:DOC 页数:8 大小:115.50KB
返回 下载 相关 举报
云大设计模式实验4单件模式_第1页
第1页 / 共8页
云大设计模式实验4单件模式_第2页
第2页 / 共8页
云大设计模式实验4单件模式_第3页
第3页 / 共8页
云大设计模式实验4单件模式_第4页
第4页 / 共8页
云大设计模式实验4单件模式_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《云大设计模式实验4单件模式》由会员分享,可在线阅读,更多相关《云大设计模式实验4单件模式(8页珍藏版)》请在金锄头文库上搜索。

1、云南大学软件学院 实验报告课程: 设计模式 学期: 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 = Inte

2、ger.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(); tes

3、t22 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.PL

4、AIN_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)uniquein

5、stance=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

6、 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()i

7、f(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

8、;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();retur

9、n 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 t

10、est1 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 p

11、ublic 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双重加锁同步方式”。 / 文档可自由编辑打印

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

当前位置:首页 > 建筑/环境 > 综合/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号