Skip to content

Commit 90d6d69

Browse files
committed
fix TurnArray decrease size
1 parent accff47 commit 90d6d69

File tree

1 file changed

+9
-2
lines changed
  • net.lecousin.core/src/main/java/net/lecousin/framework/collections

1 file changed

+9
-2
lines changed

net.lecousin.core/src/main/java/net/lecousin/framework/collections/TurnArray.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.NoSuchElementException;
1111

1212
import net.lecousin.framework.application.LCCore;
13+
import net.lecousin.framework.concurrent.CancelException;
1314
import net.lecousin.framework.concurrent.Task;
1415
import net.lecousin.framework.exception.NoException;
1516

@@ -296,6 +297,10 @@ private void increase() {
296297
}
297298

298299
private void increase(int newSize) {
300+
if (decreaseTask != null) {
301+
decreaseTask.cancel(new CancelException("TurnArray increase again"));
302+
decreaseTask = null;
303+
}
299304
Object[] a = new Object[newSize];
300305
if (end == -1) {
301306
System.arraycopy(array, start, a, 0, array.length - start);
@@ -328,19 +333,20 @@ private void decrease() {
328333
if (newSize < minSize) newSize = minSize;
329334
if (newSize >= array.length) return;
330335
if (size() >= newSize) return;
336+
if (end == -1) return;
331337
Object[] a = new Object[newSize];
332338
if (end > start) {
333339
System.arraycopy(array, start, a, 0, end - start);
334340
end = end - start;
335-
if (end == newSize) end = -1;
336341
start = 0;
337342
} else if (end < start) {
338343
System.arraycopy(array, start, a, 0, array.length - start);
339344
if (end > 0)
340345
System.arraycopy(array, 0, a, array.length - start, end);
341346
end = array.length - start + end;
342-
if (end == newSize) end = -1;
343347
start = 0;
348+
} else {
349+
start = end = 0;
344350
}
345351
array = a;
346352
}
@@ -354,6 +360,7 @@ public DecreaseTask() {
354360
@Override
355361
public Void run() {
356362
synchronized (TurnArray.this) {
363+
if (isCancelling()) return null;
357364
decreaseTask = null;
358365
decrease();
359366
}

0 commit comments

Comments
 (0)