我有一个线程池,池大小的输入是使用spring中的@值传递的,该值的引用位于.properties文件中。如下图所示:
@Value("${project.threadPoolSize}")
private static int threadPoolSize;
private static ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(threadPoolSize);
@PostConstruct
public void MasterProcessService() {
try {
LOGGER.debug("Entering processServices in MasterProcessThread ");当我尝试使用注释值给出线程池大小时,它只将1个线程池化,并执行休眠操作,但随后不会池化其他线程。
当我使用以下命令直接传递线程池大小时:
private static ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(11);它将所有线程池化,并按照定义执行睡眠和运行状态。
有人能帮助我在线程池大小中使用@值而不是直接定义一个数字吗?
发布于 2018-12-13 23:28:04
这都归因于两个事实:
1- @Value在静态字段上不起作用。如果你想用值填充它--不要让它成为静态的。
@Value("${project.threadPoolSize}")
private static int threadPoolSize;2-在用值填充threadPoolSize之前,首先创建静态threadPool变量(如果它还不是静态变量)。
如果您需要通过@Value为某个静态字段设置值,可以尝试这样做:
private static ScheduledExecutorService threadPool;
@Value("${project.threadPoolSize}")
public void setThreadPool(Integer poolSize) {
threadPool = Executors.newScheduledThreadPool(poolSize);
}值注入将在启动时调用,并将调用setThreadPool方法,该方法将使用定义的池大小初始化静态变量。
发布于 2018-12-13 23:52:46
Spring不允许向静态变量注入值。请改用java.lang.Integer。
https://stackoverflow.com/questions/53764954
复制相似问题