Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronize writing thread with existing writers #634

Merged
merged 1 commit into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions configuration/spotbugs-filters.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,10 @@
<!-- Allow writing to static field org.tinylog.policies.DynamicPolicy.reset from instance method org.tinylog.policies.DynamicPolicy.reset -->
<Bug pattern="ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD" />
</Match>
<Match>
<!-- Writing Thread -->
<Class name="org.tinylog.core.WritingThread" />
<!-- There is only one writing thread instance to wake up -->
<Bug pattern="NO_NOTIFY_NOT_NOTIFYALL" />
</Match>
</FindBugsFilter>
28 changes: 12 additions & 16 deletions tinylog-impl/src/main/java/org/tinylog/core/WritingThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
public final class WritingThread extends Thread {

private static final String THREAD_NAME = "tinylog-WritingThread";
private static final long MILLISECONDS_TO_SLEEP = 10L;

private final Object mutex;
private final Collection<Writer> writers;
private List<Task> tasks;
Expand Down Expand Up @@ -67,12 +65,6 @@ public void run() {

flush(writers);
writers.clear();

try {
sleep(MILLISECONDS_TO_SLEEP);
} catch (InterruptedException ex) {
// Ignore and continue
}
}
}

Expand All @@ -88,6 +80,7 @@ public void add(final Writer writer, final LogEntry logEntry) {
Task task = new Task(writer, logEntry);
synchronized (mutex) {
tasks.add(task);
mutex.notify();
}
}

Expand All @@ -102,9 +95,8 @@ public void add(final Writer writer, final LogEntry logEntry) {
public void shutdown() {
synchronized (mutex) {
tasks.add(Task.POISON);
mutex.notify();
}

interrupt();
}

/**
Expand All @@ -114,13 +106,17 @@ public void shutdown() {
*/
private List<Task> receiveTasks() {
synchronized (mutex) {
if (tasks.isEmpty()) {
return Collections.emptyList();
} else {
List<Task> currentTasks = tasks;
tasks = new ArrayList<Task>();
return currentTasks;
while (tasks.isEmpty()) {
try {
mutex.wait();
} catch (InterruptedException ex) {
return Collections.emptyList();
}
}

List<Task> currentTasks = tasks;
tasks = new ArrayList<Task>();
return currentTasks;
}
}

Expand Down
Loading