Skip to content

Commit 8076e27

Browse files
author
Sital Kedia
committed
Remove unneeded synchronization from public apis
1 parent c7b60a5 commit 8076e27

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

core/src/main/java/org/apache/spark/io/ReadAheadInputStream.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public class ReadAheadInputStream extends InputStream {
6969

7070
private final Condition asyncReadComplete = stateChangeLock.newCondition();
7171

72-
private final byte[] oneByte = new byte[1];
72+
private static final ThreadLocal<byte[]> oneByte = ThreadLocal.withInitial(() -> new byte[1]);
7373

7474
/**
7575
* Creates a <code>ReadAheadInputStream</code> with the specified buffer size and read-ahead
@@ -95,7 +95,7 @@ public ReadAheadInputStream(InputStream inputStream, int bufferSizeInBytes, int
9595
}
9696

9797
private boolean isEndOfStream() {
98-
if(!activeBuffer.hasRemaining() && !readAheadBuffer.hasRemaining() && endOfStream) {
98+
if (!activeBuffer.hasRemaining() && !readAheadBuffer.hasRemaining() && endOfStream) {
9999
return true;
100100
}
101101
return false;
@@ -171,16 +171,16 @@ private void waitForAsyncReadComplete() {
171171
}
172172

173173
@Override
174-
public synchronized int read() throws IOException {
175-
int val = read(oneByte, 0, 1);
174+
public int read() throws IOException {
175+
int val = read(oneByte.get(), 0, 1);
176176
if (val == -1) {
177177
return -1;
178178
}
179-
return oneByte[0] & 0xFF;
179+
return oneByte.get()[0] & 0xFF;
180180
}
181181

182182
@Override
183-
public synchronized int read(byte[] b, int offset, int len) throws IOException {
183+
public int read(byte[] b, int offset, int len) throws IOException {
184184
if (offset < 0 || len < 0 || offset + len < 0 || offset + len > b.length) {
185185
throw new IndexOutOfBoundsException();
186186
}
@@ -231,7 +231,7 @@ private int readInternal(byte[] b, int offset, int len) throws IOException {
231231
}
232232

233233
@Override
234-
public synchronized int available() throws IOException {
234+
public int available() throws IOException {
235235
stateChangeLock.lock();
236236
// Make sure we have no integer overflow.
237237
int val = (int) Math.min((long) Integer.MAX_VALUE,
@@ -241,7 +241,7 @@ public synchronized int available() throws IOException {
241241
}
242242

243243
@Override
244-
public synchronized long skip(long n) throws IOException {
244+
public long skip(long n) throws IOException {
245245
if (n <= 0L) {
246246
return 0L;
247247
}
@@ -296,7 +296,7 @@ private long skipInternal(long n) throws IOException {
296296
}
297297

298298
@Override
299-
public synchronized void close() throws IOException {
299+
public void close() throws IOException {
300300
executorService.shutdown();
301301
try {
302302
executorService.awaitTermination(10, TimeUnit.MILLISECONDS);

0 commit comments

Comments
 (0)