zoukankan      html  css  js  c++  java
  • Google线程框架com.google.guava

    pom.xml

    <dependency> <groupId>com.google.guava</groupId> <artifactId>guava-parent</artifactId> <version>23.6-jre</version> </dependency>package cc.zeelan.common.pool;
    import com.google.common.util.concurrent.ListeningExecutorService;
    import com.google.common.util.concurrent.MoreExecutors; 
    
    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledThreadPoolExecutor;
    
    public final class ThreadPooBuild {
        
        private static ThreadLocal<ThreadPooBuild> instance = new ThreadLocal<ThreadPooBuild>();
        protected static ListeningExecutorService service = null;
        protected static ScheduledThreadPoolExecutor scheduledThreadPool = null;
    
        public static ListeningExecutorService getService() {
            return service;
        }
    
        public static ScheduledThreadPoolExecutor getScheduledThreadPool() {
            return scheduledThreadPool;
        }
    
        static {
            if ((instance.get() == null) || (service == null)) {
                syncInit();
            }
        }
    
        public static void shutdown() {
            service.shutdown();
        }
    
        private static void syncInit() {
            service = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
            scheduledThreadPool = new ScheduledThreadPoolExecutor(16);
            scheduledThreadPool.setRemoveOnCancelPolicy(true);
        }
    }

     
    ================================================================================================================================调用

    package isetting.controller;
    
    import java.util.HashSet;
    import java.util.Set;
    import java.util.UUID;
    
    import com.google.common.util.concurrent.ListeningExecutorService;
    
    import cc.zeelan.common.pool.ThreadPooBuild;
    
    /**
    * 订单编号测试
    * 
    * @project isetting
    * @fileName ThreadTest.java
    * @Description
    * @author light-zhang
    * @date 2018年4月2日下午4:56:50
    * @version 1.0.0
    */
    public class ThreadTest {
    public static void main(String[] args) {
    /*
    * System.out.println((System.currentTimeMillis()+"").substring(5));
    * System.out.println( (System.nanoTime()+"").substring(7,10));
    */
    
    for (int i = 0; i < 1; i++) {
    ThreadTest test = new ThreadTest();
    test.task();
    }
    }
    
    public void task() {
    ListeningExecutorService service = ThreadPooBuild.getService();
    service.submit(new Runnable() { 
    Set<Object> data = new HashSet<Object>(); 
    @Override
    public void run() {
    long startTime = System.currentTimeMillis();
    for (int i = 0; i < 2000000; i++) {
    data.add(createOrderCode(UUID.randomUUID().toString().replace("-", "")));
    }
    System.out.println(data.size());
    service.shutdown();
    System.out.println("線程執行的時間===== " + ((System.currentTimeMillis() - startTime) / 1000) + "/s");
    }
    });
    }
    
    /**
    * 订单号生成
    * 
    * @param orderId
    * @return
    */
    public String createOrderCode(String orderId) {
    int currentTimeMillis_substr = 8;
    int nanoTime_start = 7;
    int nanoTime_end = 10;
    return Math.abs(orderId.hashCode())
    + Long.toString(System.currentTimeMillis()).substring(currentTimeMillis_substr)
    + Long.toString(System.nanoTime()).substring(nanoTime_start, nanoTime_end);
    }
    }


  • 相关阅读:
    emoji表情,直接存入数据库报错,,出现java.sql.SQLException: Incorrect string value: 'xF0x9Fx98x8ExF0。。。。。。
    Springmvc的服务端数据验证-----Hibernate Validator
    HashMap
    Treeset的两种排序方法(自然排序和比较器排序)
    Java设计模式之适配器模式
    Java中的三种工厂模式
    构造方法私有化_骰子
    Java中equals的覆盖
    HttpClient请求
    JAVA的单例模式与延时加载
  • 原文地址:https://www.cnblogs.com/light-zhang/p/8715160.html
Copyright © 2011-2022 走看看