Google線程架構com.google.guava

來源:互聯網
上載者:User

標籤: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>(); @Overridepublic 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.