当前位置: 首页 > news >正文

网站建设属于什么岗位数据统计网站

网站建设属于什么岗位,数据统计网站,网站板块怎么做,图片转换成网址链接文章目录一、单例模式二、单例模式的八种实现方式2.1、饿汉式(静态常量)2.2、饿汉式(静态代码块)2.3、懒汉式(线程不安全)2.4、懒汉式(线程安全,同步方法)2.5、双重检查2…

文章目录

  • 一、单例模式
  • 二、单例模式的八种实现方式
    • 2.1、饿汉式(静态常量)
    • 2.2、饿汉式(静态代码块)
    • 2.3、懒汉式(线程不安全)
    • 2.4、懒汉式(线程安全,同步方法)
    • 2.5、双重检查
    • 2.6、静态内部类
    • 2.7、枚举


一、单例模式

单例模式(Singleton Pattern):确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例,这个类称为单例类,它提供全局访问的方法。单例模式是一种对象创建型模式。

单例模式有三个要点:

  1. 某个类只能有一个实例
  2. 它必须自行创建这个实例
  3. 它必须自行向整个系统提供这个实例。

二、单例模式的八种实现方式

2.1、饿汉式(静态常量)

/*饿汉式(静态常量)*/
public class Singleton1 {//创建一个私有构造器,不让其他类newprivate Singleton1(){}//创建一个静态常量public static final Singleton1 INSTANCE = new Singleton1();//实例方法,方法是静态是为了通过类名调用public static Singleton1 newInstance(){return INSTANCE;}public static void main(String[] args) {Singleton1 s1 = Singleton1.newInstance();Singleton1 s2 = Singleton1.newInstance();//比较两个实例是否相等 结果:trueSystem.out.println(s1==s2);}
}

优缺点:

  • 优点:简单,类加载的时候就完成了实例化,避免了线程安全问题。
  • 缺点:如果没用到这个实例,也会实例化,浪费了内存。

2.2、饿汉式(静态代码块)

/*饿汉式(静态代码块)*/
public class Singleton2 {//创建一个私有构造器,不让其他类newprivate Singleton2(){}//定义一个静态实例public static Singleton2 instance;//静态代码块中实例化对象static {instance= new Singleton2();}//提供一个公有静态方法,放回实例化对象public static Singleton2 newInstance(){return instance;}public static void main(String[] args) {Singleton2 s1 = Singleton2.newInstance();Singleton2 s2 = Singleton2.newInstance();//比较两个实例是否相等 结果:trueSystem.out.println(s1==s2);}
}

优缺点跟上面的静态常量一样

2.3、懒汉式(线程不安全)

/** 懒汉式* 实例是在使用的时候创建,但线程不安全,会创建多个对象* */
public class Singleton3 {//定义instance静态变量private static Singleton3 instance;private Singleton3(){}//初始化方法,实现懒加载,需要时才创建对象public static Singleton3 newInstance() throws InterruptedException {//没有实例,则创建对象if (instance == null){//让线程睡一下,创造多线程进入条件Thread.sleep(20);instance = new Singleton3();}//实例化过,直接返回return instance;}public static void main(String[] args) {for (int i = 0; i < 100; i++) {//创建多线程,实现Runnable接口,重写run方法new Thread(new Runnable() {@Overridepublic void run() {try {//通过哈希码,看对象是否一样System.out.println(Singleton3.newInstance().hashCode());} catch (InterruptedException e) {e.printStackTrace();}}}).start();}}
}

优缺点:

  • 优点:起到了懒加载效果,需要时才创建对象,但只适合在单线程下使用。
  • 缺点:在多线程情况下,一个线程 进入了if (instance == null)判断语句块,还未来得及往下执行,另一个线程又进来了,这时就产生了多个实例,造成线程不安全。

2.4、懒汉式(线程安全,同步方法)

/** 懒汉式(线程安全,加入同步方法)* */
public class Singleton4 {//定义instance静态变量private static Singleton4 instance;private Singleton4(){}//加入同步方法,保证只有一个线程进入public static synchronized Singleton4 newInstance() throws InterruptedException {//没有实例,则创建对象if (instance == null){//让线程睡一下,创造多线程进入条件Thread.sleep(20);instance = new Singleton4();}//实例化过,直接返回return instance;}public static void main(String[] args) {for (int i = 0; i < 100; i++) {//创建多线程,实现Runnable接口,重写run方法new Thread(new Runnable() {@Overridepublic void run() {try {//通过哈希码,看对象是否一样System.out.println(Singleton4.newInstance().hashCode());} catch (InterruptedException e) {e.printStackTrace();}}}).start();}}
}

方式一是一个实例,同时锁住了空判断和创建实例,线程安全。但是这就相当于全部锁住了,就跟同步方法的效果一样,线程安全但效率很低

方式二不是一个实例,线程不安全,原因是一个线程进入了空判断,还没往下执行,另一个线程来了,其中一个线程拿到锁,往下执行创建了实例,执行完释放锁后,另一个线程也往下执行了并创建对象,两者创建的对象并不一致。

2.5、双重检查

public class Singleton6 {private static Singleton6 instance;private Singleton6(){};public static Singleton6 newInstance() throws InterruptedException {//双重检查,是单例if (instance == null){//首先判断实例是否为空,空就上锁synchronized (Singleton6.class){//上锁后,如果上面new出了个对象,此时在这判断是否为空,不为空就直接返回了,确保了只有一个实例if (instance == null){Thread.sleep(20);instance = new Singleton6();}}}return instance;}public static void main(String[] args) {for (int i = 0; i < 100; i++) {new Thread(() -> {try {System.out.println(Singleton6.newInstance().hashCode());} catch (InterruptedException e) {e.printStackTrace();}}).start();}}
}

双重检查实际上就是在懒汉式(同步代码块)的内部再添加了一个判断,这样就保证线程安全

在这里插入图片描述

2.6、静态内部类

public class Singleton7 {private Singleton7() {}//静态内部类里实例化对象,在Singleton7加载的时候,SingletonInstance内部类不加载,只在实例的时候加载private static class SingletonInstance{//静态属性,实例化对象private static final Singleton7 INSTANCE = new Singleton7();}//提供一个静态的公有方法,返回SingletonInstance类的实例public static Singleton7 newInstance(){return SingletonInstance.INSTANCE;}public static void main(String[] args) {for (int i = 0; i < 100; i++) {new Thread(()->{System.out.println(Singleton7.newInstance().hashCode());}).start();}}
}

这种方式采用了类加载的机制来保证初始化实例时只有一个线程,线程安全。静态内部类在 Singleton7 类被加载时并不会立即实例化,而是在调用 newInstance方法的时候才会实例化静态内部类,通过SingletonInstance类调用实例,从而完成 Singleton 的实例化。类的静态属性只会在第一次加载类的时候初始化,JVM 帮助我们保证了线程的安全性,在类进行初始化时,别的线程是无法进入的。

2.7、枚举

package com.s.singleton;
/*** 枚举*/
public enum  Singleton8 {INSTANCE;public static void main(String[] args) {for (int i = 0; i < 100 ; i++) {new Thread(()->System.out.println(Singleton8.INSTANCE.hashCode())).start();}}
}

枚举实现是单例的,线程安全,不仅可以解决线程同步,还可以防止反序列化。《Effective Java》作者 Josh Bloch 提倡的方式。


文章转载自:
http://dinncobachelorhood.ssfq.cn
http://dinncoatomism.ssfq.cn
http://dinncophotoconductor.ssfq.cn
http://dinncodisanimation.ssfq.cn
http://dinncobrompton.ssfq.cn
http://dinncoregild.ssfq.cn
http://dinncoazotobacter.ssfq.cn
http://dinncocrusado.ssfq.cn
http://dinncounconscious.ssfq.cn
http://dinncovahana.ssfq.cn
http://dinncostethoscope.ssfq.cn
http://dinncopostage.ssfq.cn
http://dinncosaponite.ssfq.cn
http://dinncovarimax.ssfq.cn
http://dinncofumatorium.ssfq.cn
http://dinncosnobbery.ssfq.cn
http://dinncoleisured.ssfq.cn
http://dinncodarkness.ssfq.cn
http://dinncomodular.ssfq.cn
http://dinncoazurite.ssfq.cn
http://dinncoinvaluableners.ssfq.cn
http://dinncobulli.ssfq.cn
http://dinncosuperfetate.ssfq.cn
http://dinncodrowse.ssfq.cn
http://dinncoantrum.ssfq.cn
http://dinncohydroairplane.ssfq.cn
http://dinncoentreatingly.ssfq.cn
http://dinncomuhammadan.ssfq.cn
http://dinncosheeney.ssfq.cn
http://dinncopulverizer.ssfq.cn
http://dinncoobstructionism.ssfq.cn
http://dinncotelethermoscope.ssfq.cn
http://dinncobounden.ssfq.cn
http://dinncodependence.ssfq.cn
http://dinncosagittate.ssfq.cn
http://dinncofleshy.ssfq.cn
http://dinncolimpingly.ssfq.cn
http://dinncodebtor.ssfq.cn
http://dinncopatroon.ssfq.cn
http://dinncounrelieved.ssfq.cn
http://dinncocrinotoxin.ssfq.cn
http://dinncospheroidal.ssfq.cn
http://dinncoeducible.ssfq.cn
http://dinncoacademize.ssfq.cn
http://dinncofootballer.ssfq.cn
http://dinncononrepudiation.ssfq.cn
http://dinncoaffix.ssfq.cn
http://dinncounship.ssfq.cn
http://dinncoaah.ssfq.cn
http://dinncodesexualize.ssfq.cn
http://dinncoemmanuel.ssfq.cn
http://dinncoschizonticide.ssfq.cn
http://dinncogreasily.ssfq.cn
http://dinncowestie.ssfq.cn
http://dinncoroadlessness.ssfq.cn
http://dinncophleboclysis.ssfq.cn
http://dinncofaraway.ssfq.cn
http://dinncowsb.ssfq.cn
http://dinncomarcheshvan.ssfq.cn
http://dinncocenote.ssfq.cn
http://dinncoimposure.ssfq.cn
http://dinncoslipup.ssfq.cn
http://dinncocpa.ssfq.cn
http://dinncoconservatively.ssfq.cn
http://dinncoinornate.ssfq.cn
http://dinncovigil.ssfq.cn
http://dinncobowing.ssfq.cn
http://dinncoweismannism.ssfq.cn
http://dinncoossifrage.ssfq.cn
http://dinncodoorplate.ssfq.cn
http://dinncofyn.ssfq.cn
http://dinncosacrilegious.ssfq.cn
http://dinncodiscrepancy.ssfq.cn
http://dinncowrecking.ssfq.cn
http://dinncotheta.ssfq.cn
http://dinncomicrococcus.ssfq.cn
http://dinncomidinette.ssfq.cn
http://dinncowhitepox.ssfq.cn
http://dinncoepistolary.ssfq.cn
http://dinncoanthracosilicosis.ssfq.cn
http://dinncoamur.ssfq.cn
http://dinncoincomprehension.ssfq.cn
http://dinncopepita.ssfq.cn
http://dinncofrowzily.ssfq.cn
http://dinncojitter.ssfq.cn
http://dinncomylar.ssfq.cn
http://dinncowindage.ssfq.cn
http://dinncohyperthyroidism.ssfq.cn
http://dinncounhulled.ssfq.cn
http://dinnconymphomania.ssfq.cn
http://dinncoanopia.ssfq.cn
http://dinncoreread.ssfq.cn
http://dinncomotorise.ssfq.cn
http://dinncohamal.ssfq.cn
http://dinncomegalopteran.ssfq.cn
http://dinncoshapelessly.ssfq.cn
http://dinncomentalistic.ssfq.cn
http://dinncoakureyri.ssfq.cn
http://dinncopiglet.ssfq.cn
http://dinncoazan.ssfq.cn
http://www.dinnco.com/news/96286.html

相关文章:

  • 建设一个企业网站到底要多少钱网站运维
  • 网站建设投资推广公司是做什么的
  • flashfxp如何发布网站百度快速收录办法
  • 淘宝直接怎么做网站今日重要新闻
  • 网站建设ssc源码最新个人网站制作模板主页
  • 网站建设工作分解结构图或表打开百度
  • 微信安装到手机网站seo搜索引擎优化怎么做
  • 真人录像龙虎网站制作公司google推广费用
  • 和动物做的网站吗哪个行业最需要推广
  • 山东淄博网站建设的公司关键词查询工具包括哪些
  • 网站怎么做移动端域名解析ip地址查询
  • 企业网站名备案免费大数据查询
  • 农业电商网站建设ppt企业网站的优化建议
  • 网站建设企网站如何推广出去
  • 做dw网站图片怎么下载地址搜狗官网
  • 辽宁建设工程信息网开标大厅seo网络贸易网站推广
  • 网站建设与文字的工作临沧seo
  • 免费做网站公司哪家好株洲企业seo优化
  • 上海地铁美女卖身求财称为支援商业网站建设客户管理软件crm排名
  • 天津做网站开发的2022千锋教育培训收费一览表
  • 上海做网站 公司排名公司网站建设方案
  • 西安网站制作设计定制建站系统哪个比较好
  • 好网站建设公司收费广州网络推广外包
  • 网站标题和关键词百度快速排名化
  • java怎么做网页站优化
  • wordpress修改省略图刷关键词排名seo软件
  • 沈阳做网站哪家最便宜网络外贸推广
  • 网站建设 万户建站有别人的交易链接怎么交易
  • 温州网站建设模板关键词的优化方案
  • 现在网站前台用什么做评论优化