Skip to content

Commit 2506b5a

Browse files
committed
Changed to use random sleeping, using a possibility.
1 parent 0f6c85d commit 2506b5a

1 file changed

Lines changed: 28 additions & 22 deletions

File tree

src/main/java/com/github/difflib/algorithm/jgit/HistogramDiff.java

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.ArrayList;
2323
import java.util.List;
2424
import java.util.Objects;
25+
2526
import org.eclipse.jgit.diff.Edit;
2627
import org.eclipse.jgit.diff.EditList;
2728
import org.eclipse.jgit.diff.Sequence;
@@ -35,7 +36,7 @@
3536
public class HistogramDiff<T> implements DiffAlgorithm<T> {
3637

3738
private long sleepMillis = 0; // Need to be 0 to disable sleeping
38-
private long millisWithoutSleep;
39+
private float diffSleepPossibility;
3940

4041
/**
4142
* Default constructor, without CPU sleeping.
@@ -47,22 +48,22 @@ public HistogramDiff() {
4748
* Enable CPU sleeping based on provided parameters.
4849
*
4950
* @param sleepMillis How long to sleep in millis
50-
* @param millisWithoutSleep How long to wait until the next sleep in millis
51+
* @param diffSleepPossibility Possibility to sleep. Number from 0 to 1. Closer to 1, higher the possibility.
5152
*/
52-
public HistogramDiff(long sleepMillis, long millisWithoutSleep) {
53+
public HistogramDiff(long sleepMillis, float diffSleepPossibility) {
5354
this.sleepMillis = sleepMillis;
54-
this.millisWithoutSleep = millisWithoutSleep;
55+
this.diffSleepPossibility = diffSleepPossibility;
5556
}
5657

5758
/**
58-
* Enable CPU sleeping for 1ms every 10ms
59+
* Enable CPU sleeping for 1ms with possibility 5%
5960
*
6061
* @param avoidCpuDrain Enable CPU sleeping or not
6162
*/
6263
public HistogramDiff(boolean avoidCpuDrain) {
6364
if (avoidCpuDrain) {
6465
this.sleepMillis = 1;
65-
this.millisWithoutSleep = 10;
66+
this.diffSleepPossibility = 0.05f;
6667
}
6768
}
6869

@@ -72,7 +73,7 @@ public List<Change> diff(List<T> original, List<T> revised) throws DiffException
7273
Objects.requireNonNull(revised, "revised list must not be null");
7374
EditList diffList = new EditList();
7475
diffList.addAll(new org.eclipse.jgit.diff.HistogramDiff().diff(
75-
new DataListComparator<>(sleepMillis, millisWithoutSleep),
76+
new DataListComparator<>(sleepMillis, diffSleepPossibility),
7677
new DataList<>(original), new DataList<>(revised)));
7778
List<Change> patch = new ArrayList<>();
7879
for (Edit edit : diffList) {
@@ -97,27 +98,16 @@ public List<Change> diff(List<T> original, List<T> revised) throws DiffException
9798
class DataListComparator<T> extends SequenceComparator<DataList<T>> {
9899

99100
private long sleepMillis;
100-
private long millisWithoutSleep;
101-
private long timePoint = System.currentTimeMillis();
101+
private float diffSleepPossibility;
102102

103-
public DataListComparator(long sleepMillis, long millisWithoutSleep) {
103+
public DataListComparator(long sleepMillis, float diffSleepPossibility) {
104104
this.sleepMillis = sleepMillis;
105-
this.millisWithoutSleep = millisWithoutSleep;
105+
this.diffSleepPossibility = diffSleepPossibility;
106106
}
107107

108108
@Override
109109
public boolean equals(DataList<T> original, int orgIdx, DataList<T> revised, int revIdx) {
110-
if (sleepMillis > 0) {
111-
if (System.currentTimeMillis() - timePoint > millisWithoutSleep) {
112-
try {
113-
Thread.sleep(sleepMillis);
114-
} catch (InterruptedException ie) {
115-
// Nothing to do
116-
}
117-
// Note that we need to call the currentTimeMillis after sleeping to get the new time
118-
timePoint = System.currentTimeMillis();
119-
}
120-
}
110+
sleepRandomly(diffSleepPossibility, sleepMillis);
121111
return original.data.get(orgIdx).equals(revised.data.get(revIdx));
122112
}
123113

@@ -126,6 +116,22 @@ public int hash(DataList<T> s, int i) {
126116
return s.data.get(i).hashCode();
127117
}
128118

119+
/**
120+
* Sleep randomly based on given possibility
121+
*
122+
* @param possibility A number from 0 to 1. The biggest, the most probable it is to sleep.
123+
* @param sleep Time to sleep in millis
124+
*/
125+
private void sleepRandomly(double possibility, long sleep) {
126+
if (Math.random() < possibility) {
127+
try {
128+
Thread.sleep(sleep);
129+
} catch (InterruptedException ie) {
130+
// Nothing to do
131+
}
132+
}
133+
}
134+
129135
}
130136

131137
class DataList<T> extends Sequence {

0 commit comments

Comments
 (0)