码迷,mamicode.com
首页 > 编程语言 > 详细

Google线程框架com.google.guava

时间:2018-04-04 10:31:54      阅读:516      评论:0      收藏:0      [点我收藏+]

标签:ble   ace   rtti   data   start   version   uid   object   run   

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

 

 



 

Google线程框架com.google.guava

标签:ble   ace   rtti   data   start   version   uid   object   run   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!