Alibaba Recommends Thread Creation
Keywords:
Java
Apache

Thread pool tool class
package com.shuige.business.train.util;
import org.apache.poi.ss.formula.functions.T;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* Description:Thread pool conforming to Alibaba specification
* User: zhouzhou
* Date: 2019-01-15
* Time: 14:19
*/
public class ThreadPoolUtil {
public static ThreadPoolExecutor threadPool;
/**
* Direct execution without return value, fucking
* @param runnable
*/
public static void execute(Runnable runnable){
getThreadPool().execute(runnable);
}
/**
* Return value is executed directly, fucking
* @param callable
*/
public static <T> Future<T> submit(Callable<T> callable){
return getThreadPool().submit(callable);
}
/**
* dcs Get the thread pool
* @return Thread pool object
*/
public static ThreadPoolExecutor getThreadPool() {
if (threadPool != null) {
return threadPool;
} else {
synchronized (ThreadPoolUtil.class) {
if (threadPool == null) {
threadPool = new ThreadPoolExecutor(8, 16, 60, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(32), new ThreadPoolExecutor.CallerRunsPolicy());
}
return threadPool;
}
}
}
}
Callable interface: Returns execution results
class TestCallable implements Callable<String> {
private String message;
public TestCallable(String message) {
this.message = message;
}
@Override
public String call() throws Exception {
Thread.sleep(300);
System.out.println(String.format("Print message%s", message));
return "OK";
}
}
Or use anonymous internal classes (recommended)
/**
* Description:
* User: zhouzhou
* Date: 2018-08-23
* Time: 13:28
*/
public class ThreadDemoTest {
@Test
public void test() throws Exception{
long start = System.currentTimeMillis();
List<Future> futureList = new ArrayList();
// Send 10 messages
for (int i = 0; i < 10; i++) {
try {
String msg = String.format("This is the first{%s}Message", i);
Future<String> messageFuture = ThreadPoolUtils.submit(new Callable<String>() {
@Override
public String call() throws Exception {
Thread.sleep(300);
System.out.println(String.format("Print message%s", msg));
return "OK";
}
});
futureList.add(messageFuture);
} catch (Exception e) {
e.printStackTrace();
}
}
for (Future<String> message : futureList) {
String messageData = message.get();
}
System.out.println(String.format("Total time-consuming{%s}Millisecond", System.currentTimeMillis() - start));
}
}
Test class:
@Test
public void test() throws Exception{
long start = System.currentTimeMillis();
List<Future> futureList = new ArrayList();
// Send 10 messages
for (int i = 0; i < 10; i++) {
try {
Future<String> messageFuture = ThreadPoolUtils.submit(new TestCallable(String.format("This is the first{%s}Message", i)));
futureList.add(messageFuture);
} catch (Exception e) {
e.printStackTrace();
}
}
for (Future<String> message : futureList) {
String messageData = message.get();
}
System.out.println(String.format("Total time-consuming{%s}Millisecond", System.currentTimeMillis() - start));
}
Implementation results:
Print the message. This is the {2} message.
Print the message. This is the {0} message.
Print the message. This is the {4} message.
Print the message. This is the {1} message.
Print the message. This is the {3} message.
Print the message. This is the {8} message.
Print the message. This is the {5} message.
Print the message. This is the {9} message.
Print the message. This is the {6} message.
Print the message. This is the {7} message.
Total time {661} milliseconds
Posted by bugz-2849 on Tue, 30 Jul 2019 14:35:44 -0700