zoukankan      html  css  js  c++  java
  • ThreadLocal

    import java.util.Arrays;
    
     public class TestCalc{
     
          static String[] names = new String[] { "E", "B", "C", "B", "E", "C"};  
          static int index = 0;  
      
          public static void main(String[] args) {  
        
              for (int i = 0; i < names.length; i++) {  
                  Thread t = new Thread() {  
                      @Override  
                      public void run() {  
                          //Created thread from every search
                          ThreadLocalUtils.DATA.set(this.getName());  
                          // 用SQLSearch将数据查询的逻辑部分实现代码分离  
                          System.out.println(new SQLSearch().run());  
                      }  
        
                  };  
                  t.setName(names[index++]);  
                  t.start();  
        
              }  
          }
    
    
          public static class SQLSearch implements MyRunnable {  
        
          public String run() {  
              //返回查询结果  
              return search(ThreadLocalUtils.DATA.get());  
          }  
        
          // 模拟查询  
          private String search(String name) {  
              String result=null;  
              // Sql  
              if (name.equals("B")) {  
                  result = (name + "185 TALL");  
              }  
              if (name.equals("C")) {  
                  result = (name + "160 TALL");  
              }  
              if (name.equals("E")) {  
                  result = (name + "180 TALL");  
              }  
              return result;  
        
          }  
        } 
    
        public interface MyRunnable {  
          
            public String run();  
        } 
    
        public static  class ThreadLocalUtils {  
            public static final ThreadLocal<String> DATA = new ThreadLocal<String>();  
        }
     }   
     
    
    
     
      
      
     
     
    
     
  • 相关阅读:
    KMP总结
    条件概率的计算
    EL和JSTL
    tomcat部署web项目
    web
    面试1
    面试可能会问的题
    简介
    建一个springboot项目
    刷题之牛客网
  • 原文地址:https://www.cnblogs.com/rojas/p/5341710.html
Copyright © 2011-2022 走看看