Skip to content

Commit d961983

Browse files
committed
Fix critical smells (too complex methods) reported by SonarCloud
1 parent 72ac8b4 commit d961983

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

src/main/java/eu/happycoders/sort/method/bubblesort/BubbleSortParallel.java

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,9 @@ public void sort(int[] elements) {
2323
Phaser phaser = new Phaser(numThreads);
2424
Thread[] threads = new Thread[numThreads];
2525
for (int i = 0; i < numThreads; i++) {
26-
final int threadNo = i;
27-
threads[threadNo] =
28-
new Thread(
29-
() -> {
30-
int startPos = startPositions[threadNo];
31-
int endPos = startPositions[threadNo + 1];
32-
33-
for (int round = 1; ; round++) {
34-
phaser.arriveAndAwaitAdvance();
35-
36-
boolean swapped = sortPartition(elements, startPos, endPos, false);
37-
38-
phaser.arriveAndAwaitAdvance();
39-
40-
swapped |= sortPartition(elements, startPos, endPos, true);
41-
if (swapped) lastSwappedInRound.set(round);
42-
43-
phaser.arriveAndAwaitAdvance();
44-
45-
if (lastSwappedInRound.get() < round) break;
46-
}
47-
});
26+
int startPos = startPositions[i];
27+
int endPos = startPositions[i + 1];
28+
threads[i] = createThread(elements, startPos, endPos, lastSwappedInRound, phaser);
4829
}
4930

5031
for (int i = 0; i < numThreads; i++) {
@@ -85,6 +66,27 @@ private int[] partition(int[] elements) {
8566
return startPositions;
8667
}
8768

69+
private Thread createThread(
70+
int[] elements, int startPos, int endPos, AtomicInteger lastSwappedInRound, Phaser phaser) {
71+
return new Thread(
72+
() -> {
73+
for (int round = 1; ; round++) {
74+
phaser.arriveAndAwaitAdvance();
75+
76+
boolean swapped = sortPartition(elements, startPos, endPos, false);
77+
78+
phaser.arriveAndAwaitAdvance();
79+
80+
swapped |= sortPartition(elements, startPos, endPos, true);
81+
if (swapped) lastSwappedInRound.set(round);
82+
83+
phaser.arriveAndAwaitAdvance();
84+
85+
if (lastSwappedInRound.get() < round) break;
86+
}
87+
});
88+
}
89+
8890
/**
8991
* Sorts a partition of the elements.
9092
*

0 commit comments

Comments
 (0)