通过ExecutorService实现单例的同步访问控制

from:http://www.java2000.net/p13007

Count是一个多线程偶数计数器,通常我们应该使用synchronized,或者Lock来保证该递增操作的原子性和可见性。本例使用了单例的ExecutorService,来保证Count的递增的原子性。

  1. import java.util.concurrent.Callable;
  2. import java.util.concurrent.ExecutionException;
  3. import java.util.concurrent.ExecutorService;
  4. import java.util.concurrent.Executors;
  5. /**
  6.  * 通过ExecutorService实现单例的同步访问控制。
  7.  * 
  8.  * @author 老紫竹 JAVA世纪网(java2000.net)
  9.  * 
  10.  */
  11. public class T {
  12.   private final ExecutorService exec = Executors.newSingleThreadExecutor();
  13.   public long getCount() throws InterruptedException, ExecutionException {
  14.     return exec.submit(new Callable<Long>() {
  15.       @Override
  16.       public Long call() throws Exception {
  17.         return ++count;
  18.       }
  19.     }).get();
  20.   }
  21.   private long count;
  22.   public void add() {
  23.     exec.execute(new Runnable() {
  24.       @Override
  25.       public void run() {
  26.         count++;
  27.         System.out.println(count);
  28.       }
  29.     });
  30.   }
  31.   public static void main(String[] args) {
  32.     final T t = new T();
  33.     class TT extends Thread {
  34.       public void run() {
  35.         for (int i = 1; i <= 5; i++) {
  36.           try {
  37.             t.add();
  38.             System.out.println(t.getCount());
  39.             Thread.sleep(10);
  40.           } catch (Exception e) {
  41.             // TODO Auto-generated catch block
  42.             e.printStackTrace();
  43.           }
  44.         }
  45.       }
  46.     }
  47.     for (int i = 1; i <= 5; i++) {
  48.       new TT().start();
  49.     }
  50.   }
  51. }
运行结果
1
2
3
4
5
6
7
10
8
9
11
12
15
16
19
14
17
20
13
18
21
22
24
26
28
25
29
27
30
23
31
32
35
37
39
33
36
38
34
40
41
42
45
47
49
43
44
46
48
50


### 模式全局唯一性的验证与最佳实践 #### 1. 使用枚举实现模式 枚举类型的实现被认为是线程安全且防反序列化的最佳实践之一。Java 中可以通过 `enum` 关键字创建,这种方式天然支持序列化机制并防止反射攻击[^1]。 ```java public enum Singleton { INSTANCE; public void execute() { System.out.println("Executing singleton method."); } } ``` 上述代码展示了如何利用 Java 的枚举特性来确保的唯一性和安全性。由于 JVM 对枚举的支持,即使面对复杂的运行环境(如多线程或多类加载器),该方法依然有效。 --- #### 2. 测试的全局唯一性 为了验证模式是否真正实现了全局唯一性,可以采用以下几种测试手段: - **多线程环境下测试** 创建多个线程尝试获取对象,并确认返回的对象引用完全一致。这一步骤尤其重要,因为某些懒汉式实现可能会在线程竞争条件下破坏性质。 ```java import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.stream.IntStream; class LazySingletonTest { private static volatile Singleton lazyInstance; private LazySingletonTest() {} public static Singleton getInstance() { if (lazyInstance == null) { synchronized (LazySingletonTest.class) { if (lazyInstance == null) { lazyInstance = new Singleton(); } } } return lazyInstance; } public static class Singleton {} } public class TestSingletonUniqueness { public static void main(String[] args) throws InterruptedException { ExecutorService executor = Executors.newFixedThreadPool(10); IntStream.range(0, 10).forEach(i -> executor.submit(LazySingletonTest::getInstance)); Object firstInstance = LazySingletonTest.getInstance(); boolean allSame = true; for (int i = 0; i < 10; i++) { if (!firstInstance.equals(LazySingletonTest.getInstance())) { allSame = false; break; } } System.out.println("All instances are the same: " + allSame); executor.shutdown(); } } ``` 此代码片段模拟了多线程环境中对对象的访问情况,并通过比较不同线程中的实引用判断其一致性。 --- #### 3. 序列化与反序列化测试 对于需要支持序列化的类,应重写 `readResolve()` 方法以阻止多次实化。 ```java import java.io.*; class SerializableSingleton implements Serializable { private static final long serialVersionUID = 78945L; private static volatile SerializableSingleton instance; private SerializableSingleton() {} public static SerializableSingleton getInstance() { if (instance == null) { synchronized (SerializableSingleton.class) { if (instance == null) { instance = new SerializableSingleton(); } } } return instance; } protected Object readResolve() { return getInstance(); // Ensure only one instance exists after deserialization. } } // Testing serialization and deserialization public class SerializationTest { public static void main(String[] args) throws Exception { SerializableSingleton original = SerializableSingleton.getInstance(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(original); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); SerializableSingleton deserialized = (SerializableSingleton) ois.readObject(); System.out.println("Original and Deserialized Instances Same? " + (original == deserialized)); // Should output 'true' } } ``` 以上代码演示了如何通过自定义 `readResolve()` 方法保护免受反序列化的影响。 --- #### 4. 反射攻击防护 尽管大多数现代框架已经提供了针对反射的安全措施,但在手动实现时仍需注意防范恶意调用私有构造函数的行为。一种常见的做法是在构造函数中加入额外校验逻辑。 ```java class ReflectionProtectedSingleton { private static volatile ReflectionProtectedSingleton instance; private ReflectionProtectedSingleton() { if (ReflectionProtectedSingleton.instance != null) { throw new RuntimeException("Use getInstance() to create an object"); } } public static ReflectionProtectedSingleton getInstance() { if (instance == null) { synchronized (ReflectionProtectedSingleton.class) { if (instance == null) { instance = new ReflectionProtectedSingleton(); } } } return instance; } } ``` 当有人试图通过反射强制访问私有构造函数时,抛出异常即可中断非法操作。 --- #### 5. 类加载器隔离问题 在分布式系统或容器化部署场景下,可能存在多个类加载器加载同一个类的情况。此时即便采用了正确的同步机制,也可能因类加载器的不同而导致多个实被创建。因此,在设计高可用服务端应用时,建议优先选用基于静态成员变量的方式或者直接使用枚举形式实现。 --- ### 结论 综上所述,验证模式全局唯一性的核心在于以下几个方面: - 多线程并发条件下的行为表现; - 支持序列化后的恢复能力; - 抵御反射攻击的能力; - 考虑到跨类加载器环境的可能性。 其中,枚举方式因其简洁高效的特点成为推荐的最佳实践。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值