This repository has been archived by the owner on May 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andrey Kurilov
committed
Apr 21, 2018
1 parent
cdba0d5
commit 8b375df
Showing
14 changed files
with
130 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/com/github/akurilov/concurrent/IndexThrottle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.github.akurilov.concurrent; | ||
|
||
public interface IndexThrottle { | ||
|
||
boolean tryAcquire(final int index); | ||
|
||
int tryAcquire(final int index, final int times); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/main/java/com/github/akurilov/concurrent/SequentialWeightsThrottle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.github.akurilov.concurrent; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
Created by kurila on 29.03.16. | ||
An throttle which uses the map of weights. | ||
The throttle determines the weight for each I/O task and makes the decision. | ||
The weight is used to pass the I/O task with specific ratio for the different keys. | ||
*/ | ||
public final class SequentialWeightsThrottle { | ||
|
||
// initial weight map (constant) | ||
private final int[] weights; | ||
private final int[] remainingWeights; | ||
|
||
public SequentialWeightsThrottle(final int[] weights) | ||
throws IllegalArgumentException { | ||
this.weights = Arrays.copyOf(weights, weights.length); | ||
remainingWeights = new int[weights.length]; | ||
resetRemainingWeights(); | ||
} | ||
|
||
private void resetRemainingWeights() | ||
throws IllegalArgumentException { | ||
for(int i = 0; i < weights.length; i ++) { | ||
remainingWeights[i] = weights[i]; | ||
} | ||
} | ||
|
||
private void ensureRemainingWeights() { | ||
for(int i = 0; i < weights.length; i ++) { | ||
if(remainingWeights[i] > 0) { | ||
return; | ||
} | ||
} | ||
resetRemainingWeights(); | ||
} | ||
|
||
public final boolean tryAcquire(final int index) { | ||
synchronized(remainingWeights) { | ||
ensureRemainingWeights(); | ||
final int remainingWeight = remainingWeights[index]; | ||
if(remainingWeight > 0) { | ||
remainingWeights[index] = remainingWeight - 1; | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
public final int tryAcquire(final int index, final int times) { | ||
if(times == 0) { | ||
return 0; | ||
} | ||
synchronized(remainingWeights) { | ||
ensureRemainingWeights(); | ||
final int remainingWeight = remainingWeights[index]; | ||
if(times > remainingWeight) { | ||
remainingWeights[index] = 0; | ||
return remainingWeight; | ||
} else { | ||
remainingWeights[index] = remainingWeight - times; | ||
return times; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 0 additions & 74 deletions
74
src/main/java/com/github/akurilov/concurrent/WeightThrottle.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.