Skip to content

Commit 695cf06

Browse files
[AsyncFileHandler] use 'volatile' for fRecordBuffer
This field is accessed by various threads, whenever logging is happening. Once in a while the buffer is pushed to the writer queue, and a new empty buffer is created. Without the volatile keyword on this field, it could be that some logger threads will still see the old buffer after it has been queued, instead of seeing the newly created one. Also, for explicitness sake, mark field fFileHandler as final. It's already used as such, and is only set in the constructor, but this makes it explicit. Note: I do not think it's necessary to have other handler-related fields marked as "volatile" (fEncoding, fFilter, fErrorManager, ...), since these are only read during initialization. They can be set later, but the real effect of setting them is that the new value is propagated to the private FileHandler, where it's used. Signed-off-by: Marc Dumais <marc.dumais@ericsson.com>
1 parent 85839ff commit 695cf06

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/main/java/org/eclipse/tracecompass/traceeventlogger/AsyncFileHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
*/
8888
public class AsyncFileHandler extends StreamHandler {
8989
private static final LogRecord CLOSE_EVENT = new LogRecord(Level.FINEST, "CLOSE_EVENT"); //$NON-NLS-1$
90-
private FileHandler fFileHandler;
90+
private final FileHandler fFileHandler;
9191
private BlockingQueue<List<LogRecord>> fQueue;
9292
private Thread fWriterThread;
9393
private int fMaxSize = 1024;
@@ -100,7 +100,7 @@ public class AsyncFileHandler extends StreamHandler {
100100
private Level fLevel;
101101
private volatile boolean fIsEnabled = true;
102102

103-
private List<LogRecord> fRecordBuffer = new ArrayList<>(fMaxSize);
103+
private volatile List<LogRecord> fRecordBuffer = new ArrayList<>(fMaxSize);
104104
private Timer fTimer = new Timer(false);
105105
TimerTask fTask = new TimerTask() {
106106
@Override

0 commit comments

Comments
 (0)