Revise synchronized blocks

- Replace synchronized with Lock when guarding long-running operations
- Remove unnecessary synchronization in FileSystemWatcher
- Replace HashMap with ConcurrentHashMap in Restarter
- Remove unnecessary locking on AtomicBoolean in
  SpringApplicationBuilder
- Remove unnecessary locking in SimpleFormatter

Closes gh-36670
This commit is contained in:
Moritz Halbritter
2023-08-02 14:30:38 +02:00
parent 6506208d29
commit 497bbf9c2d
19 changed files with 380 additions and 150 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,6 +22,8 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* {@link RandomAccessData} implementation backed by a {@link RandomAccessFile}.
@@ -209,7 +211,7 @@ public class RandomAccessDataFile implements RandomAccessData {
private static final class FileAccess {
private final Object monitor = new Object();
private final Lock lock = new ReentrantLock();
private final File file;
@@ -221,11 +223,15 @@ public class RandomAccessDataFile implements RandomAccessData {
}
private int read(byte[] bytes, long position, int offset, int length) throws IOException {
synchronized (this.monitor) {
this.lock.lock();
try {
openIfNecessary();
this.randomAccessFile.seek(position);
return this.randomAccessFile.read(bytes, offset, length);
}
finally {
this.lock.unlock();
}
}
private void openIfNecessary() {
@@ -241,20 +247,28 @@ public class RandomAccessDataFile implements RandomAccessData {
}
private void close() throws IOException {
synchronized (this.monitor) {
this.lock.lock();
try {
if (this.randomAccessFile != null) {
this.randomAccessFile.close();
this.randomAccessFile = null;
}
}
finally {
this.lock.unlock();
}
}
private int readByte(long position) throws IOException {
synchronized (this.monitor) {
this.lock.lock();
try {
openIfNecessary();
this.randomAccessFile.seek(position);
return this.randomAccessFile.read();
}
finally {
this.lock.unlock();
}
}
}