




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
云南大学软件学院 实验报告课程: 设计模式 学期: 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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年社会工作者职业水平考试中级实务模拟试卷:社会工作实务服务对象需求评估试题
- 2025年营养师基础知识考核试卷:营养师心理辅导试题
- 2025年营养师基础知识考核试卷:老年营养健康管理试题
- 2025年统计学期末考试题库:统计学与可视化结合的试题案例分析
- 2025年高压电工考试题库(高压设备故障诊断)案例分析及安全操作规范试题
- 2025年护士执业资格考试题库:妇产科护理学专项护理物资管理试题
- 钢琴演奏级模拟试卷2025年:与情感表达综合试题
- 2025年大学辅导员招聘考试:心理危机干预案例分析试题库
- 2025年护士执业资格考试题库:护理伦理学专项测试题
- 2025上交所非国有产权交易合同
- FZT 34002-2016 亚麻印染布行业标准
- 晚期卵巢癌肿瘤细胞减灭术手术技巧讲义
- 支气管扩张症的自我管理策略
- 金融学信用与信用体系
- 军队文职专用简历(2023年)
- 让子弹飞 剧本
- 八年级物理上册《第一章 机械运动》单元测试卷含答案人教版
- 2023年高考物理(山东卷)真题评析及2024备考策略
- 心肾综合征诊疗进展
- 电信笔试-企业文化
- 全国身份证号地区对应表
评论
0/150
提交评论