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);
}
}


原文地址:https://www.cnblogs.com/light-zhang/p/8715160.html