• 【JAVACONCURRENT】 阻塞容器处理锁细化


    package concurrentTest;
    
    import java.util.concurrent.BrokenBarrierException;
    import java.util.concurrent.Callable;
    import java.util.concurrent.ConcurrentHashMap;
    import java.util.concurrent.CyclicBarrier;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.FutureTask;
    
    
    public class SingletonMode {
    
     private static ConcurrentHashMap<String,FutureTask<T_T>> taskMap = new ConcurrentHashMap<String,FutureTask<T_T>>();
     private static CyclicBarrier barrier = new CyclicBarrier(50);
     private static boolean signal = true;
     private static final String KEY = "KEY";
     
     public static void main(String[] args) throws InterruptedException, ExecutionException {
      
      for(int i=0;i<100;i++){
       Thread thread = new Thread(new Runnable(){
    
        @Override
        public void run() {
         
         T_T t_t = null;
         try {
          t_t = O_O(KEY);
         } catch (InterruptedException e) {
          e.printStackTrace();
         } catch (ExecutionException e) {
          e.printStackTrace();
         }
         System.out.println(Thread.currentThread().getId() + "  " + t_t.getO_0());
        }
        
        public T_T O_O(String key) throws InterruptedException, ExecutionException{
         
         try {
          barrier.await();
         } catch (BrokenBarrierException e) {
          e.printStackTrace();
         }
         FutureTask<T_T> futureTask = taskMap.get(key);
         if(futureTask != null){
          System.out.println(Thread.currentThread().getId() + " 被阻塞到这里了.1");
         }else{
          futureTask = new FutureTask<T_T>(new Task(key));
          FutureTask<T_T> beforeTask = taskMap.putIfAbsent(key, futureTask);
          if(beforeTask == null){
           beforeTask = futureTask;
           beforeTask.run();
           signal = false;
          }else{
            futureTask = beforeTask;
          }
          if(signal)
           System.out.println(Thread.currentThread().getId() + " 被阻塞到这里了.2");
         }
         return futureTask.get();
        }
        
       });
       thread.start();
      }
     }
     
    }
    
    class Task implements Callable<T_T>{
     
     private String t;
     
     public Task(String t){
      this.t = t;
     }
    
     @Override
     public T_T call() throws Exception {
      return new T_T(t);
     }
    }
    
    class T_T{
     
     private String o_0;
     
     public String getO_0() {
      return o_0;
     }
    
     public void setO_0(String o_0) {
      this.o_0 = o_0;
     }
    
     public T_T(String o_0){
      
      try {
       Thread.currentThread().sleep(1000L);
      } catch (InterruptedException e) {
       e.printStackTrace();
      }
      System.out.println(Thread.currentThread().getId() + " do it le");
      this.o_0 = o_0;
     }
    }
  • 相关阅读:
    react 给选中的li添加样式
    纯css实现移动端横向滑动列表
    从一个Git仓库转移到另外一个仓库
    create-react-app 创建react项目 多页面应用
    JetBrains出品,一款好用到爆的数据库工具
    gloox环境搭建并运行example(小白教程,有图版本)
    Dubbo服务注册原理
    永久解决 matplotlib 图例中文方块错误
    对称二叉树
    SpringBoot运行原理
  • 原文地址:https://www.cnblogs.com/lixusign/p/2557501.html
Copyright © 2020-2023  润新知