mirror of https://github.com/longtai-cn/hippo4j
parent
3501e5fd55
commit
fbed405dd9
@ -1,52 +0,0 @@
|
|||||||
package io.dynamic.threadpool.common.enums;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 队列类型枚举
|
|
||||||
*
|
|
||||||
* @author chen.ma
|
|
||||||
* @date 2021/6/25 12:30
|
|
||||||
*/
|
|
||||||
public enum QueueTypeEnum {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@link java.util.concurrent.ArrayBlockingQueue}
|
|
||||||
*/
|
|
||||||
ARRAY_BLOCKING_QUEUE(1),
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@link java.util.concurrent.LinkedBlockingQueue}
|
|
||||||
*/
|
|
||||||
LINKED_BLOCKING_QUEUE(2),
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@link java.util.concurrent.LinkedBlockingDeque}
|
|
||||||
*/
|
|
||||||
LINKED_BLOCKING_DEQUE(3),
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@link java.util.concurrent.SynchronousQueue}
|
|
||||||
*/
|
|
||||||
SYNCHRONOUS_QUEUE(4),
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@link java.util.concurrent.LinkedTransferQueue}
|
|
||||||
*/
|
|
||||||
LINKED_TRANSFER_QUEUE(5),
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@link java.util.concurrent.PriorityBlockingQueue}
|
|
||||||
*/
|
|
||||||
PRIORITY_BLOCKING_QUEUE(6),
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@link "io.dynamic.threadpool.starter.core.ResizableCapacityLinkedBlockIngQueue"}
|
|
||||||
*/
|
|
||||||
RESIZABLE_LINKED_BLOCKING_QUEUE(9);
|
|
||||||
|
|
||||||
public Integer type;
|
|
||||||
|
|
||||||
QueueTypeEnum(int type) {
|
|
||||||
this.type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,27 @@
|
|||||||
|
package io.dynamic.threadpool.starter.spi.queue;
|
||||||
|
|
||||||
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义阻塞队列
|
||||||
|
*
|
||||||
|
* @author chen.ma
|
||||||
|
* @date 2021/7/11 00:51
|
||||||
|
*/
|
||||||
|
public interface CustomBlockingQueue {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取类型
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Integer getType();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成阻塞队列
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
BlockingQueue generateBlockingQueue();
|
||||||
|
|
||||||
|
}
|
@ -1,38 +0,0 @@
|
|||||||
package io.dynamic.threadpool.starter.toolkit;
|
|
||||||
|
|
||||||
import io.dynamic.threadpool.common.enums.QueueTypeEnum;
|
|
||||||
import io.dynamic.threadpool.starter.core.ResizableCapacityLinkedBlockIngQueue;
|
|
||||||
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.concurrent.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 阻塞队列工具类
|
|
||||||
*
|
|
||||||
* @author chen.ma
|
|
||||||
* @date 2021/6/20 16:50
|
|
||||||
*/
|
|
||||||
public class BlockingQueueUtil {
|
|
||||||
|
|
||||||
public static BlockingQueue createBlockingQueue(Integer type, Integer capacity) {
|
|
||||||
BlockingQueue blockingQueue = null;
|
|
||||||
if (Objects.equals(type, QueueTypeEnum.ARRAY_BLOCKING_QUEUE.type)) {
|
|
||||||
blockingQueue = new ArrayBlockingQueue(capacity);
|
|
||||||
} else if (Objects.equals(type, QueueTypeEnum.LINKED_BLOCKING_QUEUE.type)) {
|
|
||||||
blockingQueue = new LinkedBlockingQueue(capacity);
|
|
||||||
} else if (Objects.equals(type, QueueTypeEnum.LINKED_BLOCKING_DEQUE.type)) {
|
|
||||||
blockingQueue = new LinkedBlockingDeque(capacity);
|
|
||||||
} else if (Objects.equals(type, QueueTypeEnum.SYNCHRONOUS_QUEUE.type)) {
|
|
||||||
blockingQueue = new SynchronousQueue();
|
|
||||||
} else if (Objects.equals(type, QueueTypeEnum.LINKED_TRANSFER_QUEUE.type)) {
|
|
||||||
blockingQueue = new LinkedTransferQueue();
|
|
||||||
} else if (Objects.equals(type, QueueTypeEnum.PRIORITY_BLOCKING_QUEUE.type)) {
|
|
||||||
blockingQueue = new PriorityBlockingQueue(capacity);
|
|
||||||
} else if (Objects.equals(type, QueueTypeEnum.RESIZABLE_LINKED_BLOCKING_QUEUE.type)) {
|
|
||||||
blockingQueue = new ResizableCapacityLinkedBlockIngQueue(capacity);
|
|
||||||
} else {
|
|
||||||
throw new IllegalArgumentException("未找到类型匹配的阻塞队列.");
|
|
||||||
}
|
|
||||||
return blockingQueue;
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,93 @@
|
|||||||
|
package io.dynamic.threadpool.starter.toolkit.thread;
|
||||||
|
|
||||||
|
import io.dynamic.threadpool.starter.core.ResizableCapacityLinkedBlockIngQueue;
|
||||||
|
import io.dynamic.threadpool.starter.spi.DynamicTpServiceLoader;
|
||||||
|
import io.dynamic.threadpool.starter.spi.queue.CustomBlockingQueue;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 队列类型枚举
|
||||||
|
*
|
||||||
|
* @author chen.ma
|
||||||
|
* @date 2021/6/25 12:30
|
||||||
|
*/
|
||||||
|
public enum QueueTypeEnum {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link java.util.concurrent.ArrayBlockingQueue}
|
||||||
|
*/
|
||||||
|
ARRAY_BLOCKING_QUEUE(1),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link java.util.concurrent.LinkedBlockingQueue}
|
||||||
|
*/
|
||||||
|
LINKED_BLOCKING_QUEUE(2),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link java.util.concurrent.LinkedBlockingDeque}
|
||||||
|
*/
|
||||||
|
LINKED_BLOCKING_DEQUE(3),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link java.util.concurrent.SynchronousQueue}
|
||||||
|
*/
|
||||||
|
SYNCHRONOUS_QUEUE(4),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link java.util.concurrent.LinkedTransferQueue}
|
||||||
|
*/
|
||||||
|
LINKED_TRANSFER_QUEUE(5),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link java.util.concurrent.PriorityBlockingQueue}
|
||||||
|
*/
|
||||||
|
PRIORITY_BLOCKING_QUEUE(6),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link "io.dynamic.threadpool.starter.core.ResizableCapacityLinkedBlockIngQueue"}
|
||||||
|
*/
|
||||||
|
RESIZABLE_LINKED_BLOCKING_QUEUE(9);
|
||||||
|
|
||||||
|
public Integer type;
|
||||||
|
|
||||||
|
QueueTypeEnum(int type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
static {
|
||||||
|
DynamicTpServiceLoader.register(CustomBlockingQueue.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BlockingQueue createBlockingQueue(Integer type, Integer capacity) {
|
||||||
|
BlockingQueue blockingQueue = null;
|
||||||
|
if (Objects.equals(type, ARRAY_BLOCKING_QUEUE.type)) {
|
||||||
|
blockingQueue = new ArrayBlockingQueue(capacity);
|
||||||
|
} else if (Objects.equals(type, LINKED_BLOCKING_QUEUE.type)) {
|
||||||
|
blockingQueue = new LinkedBlockingQueue(capacity);
|
||||||
|
} else if (Objects.equals(type, LINKED_BLOCKING_DEQUE.type)) {
|
||||||
|
blockingQueue = new LinkedBlockingDeque(capacity);
|
||||||
|
} else if (Objects.equals(type, SYNCHRONOUS_QUEUE.type)) {
|
||||||
|
blockingQueue = new SynchronousQueue();
|
||||||
|
} else if (Objects.equals(type, LINKED_TRANSFER_QUEUE.type)) {
|
||||||
|
blockingQueue = new LinkedTransferQueue();
|
||||||
|
} else if (Objects.equals(type, PRIORITY_BLOCKING_QUEUE.type)) {
|
||||||
|
blockingQueue = new PriorityBlockingQueue(capacity);
|
||||||
|
} else if (Objects.equals(type, RESIZABLE_LINKED_BLOCKING_QUEUE.type)) {
|
||||||
|
blockingQueue = new ResizableCapacityLinkedBlockIngQueue(capacity);
|
||||||
|
}
|
||||||
|
|
||||||
|
Collection<CustomBlockingQueue> customBlockingQueues = DynamicTpServiceLoader
|
||||||
|
.getSingletonServiceInstances(CustomBlockingQueue.class);
|
||||||
|
blockingQueue = Optional.ofNullable(blockingQueue).orElseGet(() -> customBlockingQueues.stream()
|
||||||
|
.filter(each -> Objects.equals(type, each.getType()))
|
||||||
|
.map(each -> each.generateBlockingQueue())
|
||||||
|
.findFirst()
|
||||||
|
.orElse(new LinkedBlockingQueue(capacity)));
|
||||||
|
|
||||||
|
return blockingQueue;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue