线程同步,单例模式【例子】

8-1

(1)同步代码块:

public class Ticket implements Runnable {

//100

int ticket = 100;

//定义锁对象

Object lock = new Object();

@Override

public void run() {

//模拟卖票

while(true){

//同步代码块

synchronized (lock){

if (ticket > 0) {

//模拟电影选坐的操作

try {

Thread.sleep(10);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println(Thread.currentThread().getName() + "正在卖票:" + ticket--);

}

}

}

}

}

8-2

(2)同步方法:

public class Ticket implements Runnable {

//100

int ticket = 100;

//定义锁对象

Object lock = new Object();

@Override

public void run() {

//模拟卖票

while(true){

//同步方法

method();

}

}

 

//同步方法,锁对象this

public synchronized void method(){

if (ticket > 0) {

//模拟选坐的操作

try {

Thread.sleep(10);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println(Thread.currentThread().getName() + "正在卖票:" + ticket--);

}

}

}

8-3

(3)同步锁

public class Ticket implements Runnable {

//100

int ticket = 100;

//创建Lock锁对象

Lock ck = new ReentrantLock();

@Override

public void run() {

//模拟卖票

while(true){

//synchronized (lock){

ck.lock();

if (ticket > 0) {

//模拟选坐的操作

try {

Thread.sleep(10);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println(Thread.currentThread().getName() + "正在卖票:" + ticket--);

}

ck.unlock();

//}

}

}

}

9-1

 

1. public class Singleton {  

2.     private static Singleton instance;  

3.     private Singleton (){}  

4.   

5.     public static Singleton getInstance() {  

6.     if (instance == null) {  

7.         instance = new Singleton();  

8.     }  

9.     return instance;  

10.     }  

11. }  

 

9-2

1. public class Singleton {  

2.     private static Singleton instance;  

3.     private Singleton (){}  

4.     public static synchronized Singleton getInstance() {  

5.     if (instance == null) {  

6.         instance = new Singleton();  

7.     }  

8.     return instance;  

9.     }  

10. }  

 

9-3

1. public class Singleton {  

2.     private static Singleton instance = new Singleton();  

3.     private Singleton (){}  

4.     public static Singleton getInstance() {  

5.     return instance;  

6.     }  

7. }  

 

9-4

1. public class Singleton {  

2.     private Singleton instance = null;  

3.     static {  

4.     instance = new Singleton();  

5.     }  

6.     private Singleton (){}  

7.     public static Singleton getInstance() {  

8.     return this.instance;  

9.     }  

10. }  

 

9-5

1. public class Singleton {  

2.     private static class SingletonHolder {  

3.     private static final Singleton INSTANCE = new Singleton();  

4.     }  

5.     private Singleton (){}  

6.     public static final Singleton getInstance() {  

7.     return SingletonHolder.INSTANCE;  

8.     }  

9. }  

 

9-6

1. public enum Singleton {  

2.     INSTANCE;  

3.     public void whateverMethod() {  

4.     }  

5. }  

 

9-7

1. public class Singleton {  

2.     private volatile static Singleton singleton;  

3.     private Singleton (){}  

4.     public static Singleton getSingleton() {  

5.     if (singleton == null) {  

6.         synchronized (Singleton.class) {  

7.         if (singleton == null) {  

8.             singleton = new Singleton();  

9.         }  

10.         }  

11.     }  

12.     return singleton;  

13.     }  

14. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个使用双重检查锁定方式实现的线程安全的懒汉式单例模式的示例代码: ```java public class Singleton { private static volatile Singleton instance; private Singleton() { // 私有化构造方法,防止外部实例化对象 } public static Singleton getInstance() { if (instance == null) { synchronized (Singleton.class) { if (instance == null) { instance = new Singleton(); } } } return instance; } } ``` 在该例子中,我们使用 `private static volatile` 关键字定义了一个静态的 `instance` 对象,`volatile` 关键字可以保证多线程之间的可见性。在 `getInstance()` 方法中,我们通过 `if (instance == null)` 判断该对象是否为 `null`,如果为 `null` 则进入同步代码块,在同步代码块中再次判断 `instance` 是否为 `null`,如果为 `null` 则创建一个新的实例对象并赋值给 `instance`,然后返回该实例对象。由于使用了双重检查锁定方式,因此可以保证在多线程环境下只会创建一个实例对象。 还有一种实现方式是使用静态内部,以下是使用静态内部实现的线程安全的懒汉式单例模式的示例代码: ```java public class Singleton { private Singleton() { // 私有化构造方法,防止外部实例化对象 } private static class SingletonHolder { private static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return SingletonHolder.INSTANCE; } } ``` 在该例子中,我们使用静态内部 `SingletonHolder` 来创建实例对象,由于静态内部只有在被引用时才会被加载,因此可以保证在多线程环境下只会创建一个实例对象。在 `getInstance()` 方法中,我们直接返回 `SingletonHolder.INSTANCE` 即可获取实例对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值