Skip to content

Commit

Permalink
🧵 thread access to messages synchronized #13
Browse files Browse the repository at this point in the history
Signed-off-by: Marcus Fihlon <[email protected]>
  • Loading branch information
McPringle committed Mar 27, 2024
1 parent 188084c commit 883c758
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/main/java/swiss/fihlon/apus/service/SocialService.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,17 @@ private void scheduler() {
}

private void updateMessages() {
messages = mastodonAPI.getMessages("javaland");
final var newMessages = mastodonAPI.getMessages("hackergarten");
synchronized (this) {
messages = newMessages;
}
}

public List<Message> getMessages(final int limit) {
final int toIndex = limit > 0 && limit < messages.size() ? limit : messages.size() - 1;
return Collections.unmodifiableList(limit == 0 ? messages : messages.subList(0, toIndex));
synchronized (this) {
final int toIndex = limit > 0 && limit < messages.size() ? limit : messages.size() - 1;
return Collections.unmodifiableList(limit == 0 ? messages : messages.subList(0, toIndex));
}
}

}

0 comments on commit 883c758

Please sign in to comment.