Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么slf4j记录器有时会打印出父线程,即使代码假定由子线程执行?_Java_Multithreading_Concurrency_Parallel Processing_Threadpoolexecutor - Fatal编程技术网

Java 为什么slf4j记录器有时会打印出父线程,即使代码假定由子线程执行?

Java 为什么slf4j记录器有时会打印出父线程,即使代码假定由子线程执行?,java,multithreading,concurrency,parallel-processing,threadpoolexecutor,Java,Multithreading,Concurrency,Parallel Processing,Threadpoolexecutor,以这个例子为例 @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_EN

以这个例子为例

@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));
    }

}
我们看到
BufferTask
中的代码由两个线程运行,即
main
线程和子线程
MyBufferService-thread-0
inside
main
线程

我原以为只有子线程在执行任务,但似乎父线程也在执行任务

为什么会这样?我做错了什么吗?

您设置了拒绝,让调用者运行任务。这意味着,如果任务在#execute中被拒绝,那么调用线程(即您案例中的主线程)将运行任务,这就是您看到的。更改该策略或在#execute方法上处理异常

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