日志 >
个人日记
![]()
单例模式(singleton)
![]() public class SingletonA { //私有属性 private static int id = 1; //SingletonA的唯一实例 private static SingletonA instance = new SingletonA(); //将构造函数私有,防止外界构造SingletonA实例 private SingletonA() { } /** * 获取SingletonA的实例 */ public static SingletonA getInstance() { return instance; } /** * 获取实例的id,synchronized关键字表示该方法是线程同步的, * 即任一时刻最多只能有一个线程进入该方法 * @return */ public synchronized int getId() { return id; } /** * 将实例的id加1 */ public synchronized void nextID() { id++; } } package singleton; public class SingletonB { //私有属性 private static int id = 1; //SingletonB的唯一实例 private static SingletonB instance = null; //将构造函数私有,防止外界构造SingletonB实例 private SingletonB() { } //获取SingletonB的唯一实例,同样用synchronized关键字保证某一时刻只有一个线程调用此方法。 public static synchronized SingletonB getInstance() { //如果instance为空,便创建一个新的SingletonB实例,否则,返回已有的实例 if (instance == null) { instance = new SingletonB(); } return instance; } public synchronized int getId() { return id; } public synchronized void nextID() { id++; } } package singleton; /* * 模式名称:单建模式 * 模式特征:只能创建该类的一个实例 * 模式用途:提供一个全局共享类实例 **/ public class SingletonTest { public static void main(String[] args) { // SingletonA singletonA =new SingletonA(); // SingletonA singleton2 =new SingletonA(); SingletonA a1 = SingletonA.getInstance(); SingletonA a2 = SingletonA.getInstance(); System.out.println("用SingletonA实现单例模式"); System.out.println("调用nextID方法前:"); System.out.println("a1.id=" + a1.getId()); System.out.println("a2.id=" + a2.getId()); a2.nextID(); a1.nextID(); System.out.println("调用nextID方法后:"); System.out.println("a1.id=" + a1.getId()); System.out.println("a2.id=" + a2.getId()); // SingletonA和SingletonB的区别:前者是在类被加载的时候就创建了实例, // 而后者是在调用getInstance方法时才创建实例。 // SingletonB b1 = SingletonB.getInstance(); // SingletonB b2 = SingletonB.getInstance(); // System.out.println("用SingletonB实现单例模式"); // System.out.println("调用nextID方法前:"); // System.out.println("b1.id=" + b1.getId()); // System.out.println("b2.id=" + b2.getId()); // b1.nextID(); // System.out.println("调用nextID方法后:"); // System.out.println("b1.id=" + b1.getId()); // System.out.println("b2.id=" + b2.getId()); } } ![]() |
评论列表
/x02").replace(//[//quote/]/g,"/x01"); for(var i=0;i<2;i++) s=s.replace(//x03([^/x03/x01/x02]*?)/x02([^/x03/x01/x02]*?)/x01/g, function(a,b,c){ return '
'+b+'引用内容:'; }); return s.replace(/[/x03/x02/x01]/g,""); } var bLoaded = false; function checkMsgReply(obj) { if(!bLoaded) top.includeJS('/qzone/blog/script/common.js', function(){bLoaded=true;checkMsgReply(obj)}, document); else checkReply(obj); if(obj.checked){ MAX_COMMENT_LEN = 500; } else { MAX_COMMENT_LEN = 4500; } _fontCount = MAX_COMMENT_LEN; //字数限制 if(!window.sendCommentEditor) return; if(sendCommentEditor.editorArea.editMode == 1) toCountFont(sendCommentEditor.id, "html"); else toCountFont(sendCommentEditor.id, "text"); } function showMsgLeftCnt() { if(!bLoaded) top.includeJS('/qzone/blog/script/common.js', function(){bLoaded=true;showMsgLeftCnt();}, document); else showLeftSMS(); } function selectBlogPaper() { if(checkLogin() <= 10000) { top.showLoginBox("mall"); return; } if(!!top.g_JData["blogContent"]) { if(parent.g_iLoginUin == parent.g_iUin) { location.href="/qzone/newblog/blogeditor.html?paperid=" + parent.g_JData["blogContent"].data.lp_id + "&paperstyle=" + parent.g_JData["blogContent"].data.lp_style + "&paperdialog=1"; } else { parent.location.href="http://user.qzone.qq.com/" + parent.g_iLoginUin + "/addNewBlog?paperid=" + parent.g_JData["blogContent"].data.lp_id + "&paperstyle=" + parent.g_JData["blogContent"].data.lp_style; } } else { top.showMsgBox("抱歉,暂时无法获取该信纸信息!", 1, 2000); } } /** * 批量删除中选择全选 */ function selectAllComments(bChecked) { var oList = document.getElementsByName("commentCheckBox"); if(oList.length==0) return; for(var i=0; i 0){ dalert(null, parent.g_XDoc["delBatchReply"].xml, 2000); delete parent.g_XDoc["delBatchReply"]; return; } dalert(null, parent.g_XDoc["delBatchReply"].xml, 2000, 2); contentProperty.totalCommentNumber -= nDeleteCnt; //清理cache with(contentProperty){ delete parent.g_XDoc["blogRoot"].contentHSList[currentBlogid]; pageList = {}; pageIndexMap = []; currentCommentPage = lastCommentPage = (!contentProperty.nowaPage)?0:nowaPage[3]; parent.g_XDoc["blogRoot"].replyNumUpdateHSmap[currentBlogid] = totalCommentNumber; parent.isRefreshTop = true; if(currentCommentPage == 0) { setTimeout(contentInit, 1000); } else{ var tp = Math.ceil(totalCommentNumber/PAGE_COMMENT_NUM); var num = totalCommentNumber%PAGE_COMMENT_NUM; if(num==0 || currentCommentPage10000 && top.g_iLoginUin!=top.g_iUin) { $("msgboardSelfReply").style.display = ""; $("blogSelPaper").title = "我也要使用此信纸写日志"; } setTimeout(contentInit,50); //]]> </script>'+c+'
loading......
共2篇评论,第1页/共1页
批量删除 取消批量删除
共2篇评论,第1页/共1页