作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑这个例子
@Test
public void test2() throws InterruptedException {
int MAX_ENTRIES = 2;
Map cache = new LinkedHashMap(MAX_ENTRIES+1, .75F, true) {
public boolean removeEldestEntry(Map.Entry eldest) {
return size() > MAX_ENTRIES;
}
};
MyBufferService testService = new MyBufferService("test2");
for (int i = 0; i < 100000; i++) {
testService.putBufferTask(Integer.toString(i));
}
TimeUnit.SECONDS.sleep(1);
}
public class MyBufferService {
private ThreadPoolExecutor executor;
private final Logger LOGGER = LoggerFactory.getLogger(MyBufferService.class);
private final Map cache;
final int MAX_ENTRIES = 1;
public MyBufferService(String buffName) {
executor = new ThreadPoolExecutor(1, // corePoolSize
1, // maximumPoolSize
60, TimeUnit.SECONDS, // keepAlive
new LinkedBlockingQueue<>(10000), // workQueue
new ThreadFactoryBuilder().setNameFormat(buffName + "-MyBufferService-thread-%d").build(), // factory
new ThreadPoolExecutor.CallerRunsPolicy() // rejected execution handler
);
this.cache = new LinkedHashMap(MAX_ENTRIES+1, .75F, true) {
public boolean removeEldestEntry(Map.Entry eldest) {
return size() > MAX_ENTRIES;
}
};
}
private class BufferTask implements Runnable {
private final String mystring;
private final Map cache;
BufferTask(String mystring, Map cache) throws NullPointerException {
this.mystring = mystring;
this.cache = cache;
}
@Override
public void run() {
try {
synchronized (this.cache) {
this.cache.put(this.mystring, "hi");
if (this.cache.size() > 0) {
LOGGER.info("this is size {}", this.cache.size() );
}
}
} catch (Throwable t) {
}
}
}
public void putBufferTask(
String mystring) throws RejectedExecutionException, NullPointerException {
executor.submit(new BufferTask(mystring, this.cache));
}
}
并考虑该行的以下输出片段
LOGGER.info("this is size {}", this.cache.size() );
2018-06-13 18:19:46,760 [INFO] [main] c.t.u.w.$MyBufferService - this is size 1
2018-06-13 18:19:46,761 [INFO] [main] c.t.u.w.$MyBufferService - this is size 1
2018-06-13 18:19:46,761 [INFO] [main] c.t.u.w.$MyBufferService - this is size 1
2018-06-13 18:19:46,761 [INFO] [main] c.t.u.w.$MyBufferService - this is size 1
2018-06-13 18:19:46,761 [INFO] [main] c.t.u.w.$MyBufferService - this is size 1
2018-06-13 18:19:46,761 [INFO] [test2-MyBufferService-thread-0] c.t.u.w.$MyBufferService - this is size 1
2018-06-13 18:19:46,761 [INFO] [test2-MyBufferService-thread-0] c.t.u.w.$MyBufferService - this is size 1
2018-06-13 18:19:46,761 [INFO] [test2-MyBufferService-thread-0] c.t.u.w.$MyBufferService - this is size 1
2018-06-13 18:19:46,761 [INFO] [test2-MyBufferService-thread-0] c.t.u.w.$MyBufferService - this is size 1
我们看到 BufferTask
内的代码由 2 个线程运行,其中的 main
线程和子线程 MyBufferService-thread-0
主
线程
我本以为只有子线程正在执行任务,但看起来父线程也在执行任务。
为什么会发生这种情况?我做错了什么吗?
最佳答案
您设置了拒绝excecution policy让调用者运行任务。这意味着,如果任务在 #execute 中被拒绝,则调用线程(即您的情况下的主线程)将运行该任务,这就是您所看到的。更改该策略或处理 #execute 方法上的异常。
关于java - 为什么 slf4j 记录器有时会打印出父线程,即使代码应该由子线程执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50848363/
我是一名优秀的程序员,十分优秀!