Google线程框架com.google.guava

Posted light-zhang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
}
}

 

 



 

以上是关于Google线程框架com.google.guava的主要内容,如果未能解决你的问题,请参考以下文章

《TensorFlow实战Google深度学习框架(第二版)》学习笔记及书评

《TensorFlow实战Google深度学习框架(第二版)》学习笔记及书评

如何让 Google Test 检测 Linux 上的线程数?

TensorFlow多线程输入数据处理框架——输入数据处理框架

TensorFlow多线程输入数据处理框架——组合训练数据

Service框架浅析Google-Guava Concurrent