yangxin created LOG4J2-1894: ------------------------------- Summary: Potential data race in ReconfigurationDeadlockTest Key: LOG4J2-1894 URL: https://issues.apache.org/jira/browse/LOG4J2-1894 Project: Log4j 2 Issue Type: Bug Components: Core Affects Versions: 2.8.1 Environment: Win10、java8 Reporter: yangxin
In this junit test , for the object *finished* , if read (in method testReconfig ) is before write ( in method LoggerThread ), then the program will think the loggerThread didn't finish and print error message, but the loggerThread has finished in fact. If there is a potential data race? {code:title=ReconfigurationDeadlockTest.java|borderStyle=solid} LoggerThread[] threads = new LoggerThread[THREAD_COUNT]; @Test public void testReconfig() throws InterruptedException { final Updater updater = new Updater(); for (int i = 0; i < THREAD_COUNT; ++i) { threads[i] = new LoggerThread(i); threads[i].setDaemon(true); } for (int i = 0; i < THREAD_COUNT; ++i) { threads[i].start(); } updater.setDaemon(true); updater.start(); Thread.sleep(100); boolean stillWaiting = true; for (int i = 0; i < 200; ++i) { int index = 0; for (; index < THREAD_COUNT; ++index) { if (!finished[index]) { break; } } if (index == THREAD_COUNT) { stillWaiting = false; break; } Thread.sleep(100); } updater.shutdown = true; if (stillWaiting) { final ThreadDumpMessage message = new ThreadDumpMessage("Waiting"); System.err.print(message.getFormattedMessage()); } for (int i = 0; i < THREAD_COUNT; ++i) { if (threads[i].isAlive()) { threads[i].interrupt(); } } assertFalse("loggerThread didn't finish", stillWaiting); } private class LoggerThread extends Thread { private final Logger logger = LogManager.getRootLogger(); private final int index; public LoggerThread(final int i) { index = i; } @Override public void run() { int i = 0; try { for (i=0; i < 30; ++i) { logger.error("Thread: " + index + ", Test: " + i++); } } catch (final Exception ie) { return; } finished[index] = true; } } {code} -- This message was sent by Atlassian JIRA (v6.3.15#6346)