线程唯一的单例

经典设计模式的单例模式是指进程唯一的对象实例,实现code如下:

package cun.zheng.weng.design.sinstnce;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

public class IdGenerator {

    private IdGenerator(){}

    private AtomicLong generator = new AtomicLong(0);

    static class IdGeneratorHolder {
        private final static IdGenerator ID_GENERATOR = new IdGenerator();
    }

    public static IdGenerator getInstance(){
        return IdGeneratorHolder.ID_GENERATOR;
    }

    public Long getId(){
        return generator.getAndIncrement();
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(3,3,1000, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<>());
        CountDownLatch latch = new CountDownLatch(10);
        for(int i=0;i<10;i++){
            Integer taskId = i;
            executor.submit(()->{
                Long threadId = Thread.currentThread().getId();
                System.out.println("Task "+ taskId +" generate id->" + IdGenerator.getInstance().getId() + " ,task done by "+ threadId);
                latch.countDown();
            });
        }
        latch.await();
        executor.shutdownNow();
    }
}

输出结果:
Task 0 generate id->0 ,task done by 9
Task 1 generate id->1 ,task done by 10
Task 2 generate id->2 ,task done by 11
Task 3 generate id->3 ,task done by 9
Task 5 generate id->5 ,task done by 10
Task 4 generate id->4 ,task done by 11
Task 7 generate id->7 ,task done by 10
Task 6 generate id->6 ,task done by 9
Task 9 generate id->9 ,task done by 10
Task 8 generate id->8 ,task done by 11

别跟我扯什么双重检测单例模式,性能太差,不考虑,单例模式就用静态内部类实现,高性能+延迟加载,还要什么双重检测!

那么,问题来了,如果我要实现线程绑定呢,换句话说就是实现线程间唯一的单例,进程中可以有多个实例。

思路很简单:将线程id绑定对象实例就行了。以下使用Map和JDK原生的ThreadLocal实现这个想法。

package cun.zheng.weng.design.sinstnce;

import java.util.Map;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;

public class ThreadSingleInstance {

    private ThreadSingleInstance(){}

    AtomicLong atomicLong = new AtomicLong(0);

    private static final Map idGeneratorHolder = new ConcurrentHashMap<>();

    public Long getId(){
        return atomicLong.getAndIncrement();
    }

    public static ThreadSingleInstance getInstance(){
        Long threadId = Thread.currentThread().getId();
        idGeneratorHolder.putIfAbsent(threadId,new ThreadSingleInstance());
        return idGeneratorHolder.get(threadId);
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(3,3,1000, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<>());
        CountDownLatch latch = new CountDownLatch(10);
        for(int i=0;i<10;i++){
            Integer taskId = i;
            executor.submit(()->{
                Long threadId = Thread.currentThread().getId();
                System.out.println("Task "+ taskId +" generate id->" + ThreadSingleInstance.getInstance().getId() + " ,task done by "+ threadId);
                latch.countDown();
            });
        }
        latch.await();
        executor.shutdownNow();
    }
}

输出:
Task 1 generate id->0 ,task done by 10
Task 2 generate id->0 ,task done by 11
Task 3 generate id->1 ,task done by 10
Task 4 generate id->1 ,task done by 11
Task 5 generate id->2 ,task done by 10
Task 6 generate id->2 ,task done by 11
Task 7 generate id->3 ,task done by 10
Task 8 generate id->3 ,task done by 11
Task 9 generate id->4 ,task done by 10
Task 0 generate id->0 ,task done by 9

 

package cun.zheng.weng.design.sinstnce;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

public class ThreadInstanceByThreadLocal {

    private ThreadInstanceByThreadLocal(AtomicLong atomicLong){
        this.atomicLong = atomicLong;
    }

    private AtomicLong atomicLong;

    public Long getId(){
        return atomicLong.getAndIncrement();
    }

    public static ThreadInstanceByThreadLocal getInstance(){
        return new ThreadInstanceByThreadLocal(new AtomicLong(0));
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(3,3,1000, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<>());
        CountDownLatch latch = new CountDownLatch(10);
        ThreadLocal threadLocal = new ThreadLocal(){
            @Override
            protected ThreadInstanceByThreadLocal initialValue() {
                return ThreadInstanceByThreadLocal.getInstance();
            }
        };
        for(int i=0;i<10;i++){
            Integer taskId = i;
            executor.submit(()->{
                Long threadId = Thread.currentThread().getId();
                System.out.println("Task "+ taskId +" generate id->" + threadLocal.get().getId() + " ,task done by "+ threadId);
                latch.countDown();
            });
        }
        latch.await();
        executor.shutdownNow();
    }
}

你可能感兴趣的:(java,开发语言)