本文整理汇总了Java中java.util.concurrent.ThreadLocalRandom.current方法的典型用法代码示例。如果您正苦于以下问题:Java ThreadLocalRandom.current方法的具体用法?Java ThreadLocalRandom.current怎么用?Java ThreadLocalRandom.current使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ThreadLocalRandom
的用法示例。
在下文中一共展示了ThreadLocalRandom.current方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createRandomStringBasedOnTimestamp
import java.util.concurrent.ThreadLocalRandom; //导入方法依赖的package包/类
/**
* 根据当前时间(hour)戳创建字符串,包括8位随机字符串和时间戳
*
* @return
* @throws UnsupportedEncodingException
*/
public static String createRandomStringBasedOnTimestamp(int type) throws UnsupportedEncodingException{
String timestampStr = createTimestampStr(type);
Random random = ThreadLocalRandom.current();
int p = random.nextInt(8-1);
int length = 8+timestampStr.length();
StringBuffer strBuffer = new StringBuffer(length);
for(int i=0;i<length;i++){
strBuffer.append(base[random.nextInt(base.length)]);
if(i==p){
strBuffer.append(timestampStr);
}
}
return strBuffer.toString();
}
示例2: handleDiceRoll
import java.util.concurrent.ThreadLocalRandom; //导入方法依赖的package包/类
private void handleDiceRoll(@NotNull MessageReceivedEvent event, @NotNull List<String> args) {
int upper = Integer.parseUnsignedInt(args.get(0).substring(2));
int iterations = 1;
if (args.size() > 1 && INT_PATTERN.matcher(args.get(1)).find()) {
iterations = Math.min(12, Integer.parseUnsignedInt(args.get(1)));
}
int sIterations = iterations;
ThreadLocalRandom random = ThreadLocalRandom.current();
EmbedBuilder builder = MessageUtils.getEmbedBuilder(event.getMessage().getAuthor());
int sum = 0;
while (iterations-- != 0) {
int num = random.nextInt(1, upper + 1);
sum += num;
builder.appendField(":game_die: [1," + upper + "]", num + "", true);
}
builder.withTitle("Tossed " + sIterations + (sIterations == 1 ? " dice" : " dices"));
builder.withDescription("Average result: " + (sum / sIterations));
sendThenDelete(MessageUtils.getMessageBuilder(event.getMessage())
.withEmbed(builder.build()));
delete(event.getMessage());
}
示例3: newBuilderInstance
import java.util.concurrent.ThreadLocalRandom; //导入方法依赖的package包/类
private static Builder newBuilderInstance() {
ThreadLocalRandom random = ThreadLocalRandom.current();
return new Builder(UUID.randomUUID().toString(),
random.nextInt(),
UUID.randomUUID().toString(),
Instant.ofEpochMilli(random.nextLong(0, Instant.now().toEpochMilli())));
}
示例4: testNextIntBadBounds
import java.util.concurrent.ThreadLocalRandom; //导入方法依赖的package包/类
/**
* nextInt(least >= bound) throws IllegalArgumentException
*/
public void testNextIntBadBounds() {
int[][] badBoundss = {
{ 17, 2 },
{ -42, -42 },
{ Integer.MAX_VALUE, Integer.MIN_VALUE },
};
ThreadLocalRandom rnd = ThreadLocalRandom.current();
for (int[] badBounds : badBoundss) {
try {
rnd.nextInt(badBounds[0], badBounds[1]);
shouldThrow();
} catch (IllegalArgumentException success) {}
}
}
示例5: testSortTaskDemo
import java.util.concurrent.ThreadLocalRandom; //导入方法依赖的package包/类
/**
* SortTask demo works as advertised
*/
public void testSortTaskDemo() {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
long[] array = new long[1007];
for (int i = 0; i < array.length; i++)
array[i] = rnd.nextLong();
long[] arrayClone = array.clone();
testInvokeOnPool(mainPool(), new SortTask(array));
Arrays.sort(arrayClone);
assertTrue(Arrays.equals(array, arrayClone));
}
示例6: correctnessTesting
import java.util.concurrent.ThreadLocalRandom; //导入方法依赖的package包/类
@Test
void correctnessTesting() throws Exception {
ThreadLocalRandom random = ThreadLocalRandom.current();
for(int i = 0; i < 20; i++) {
ZipfDistribution zipf = new ZipfDistribution(random.nextDouble(), 1000);
long[] original = zipf.stream().limit(2000).asLongStream().toArray();
verifyCompression(original, -1);
}
}
示例7: spread
import java.util.concurrent.ThreadLocalRandom; //导入方法依赖的package包/类
private void spread() {
World world = GameState.getGameWorld();
ThreadLocalRandom random = ThreadLocalRandom.current();
int spreadSize = UHC.getInstance().getMainConfig().getWorldBorderInitialSize();
Stream<? extends Player> players = Bukkit.getServer().getOnlinePlayers().stream().filter(player -> player.getGameMode() != GameMode.SPECTATOR);
players.forEach(player -> player.teleport(generateSafe(spreadSize, world, random)));
}
示例8: testUnsizedDoublesCountSeq
import java.util.concurrent.ThreadLocalRandom; //导入方法依赖的package包/类
/**
* A sequential unsized stream of doubles generates at least 100 values
*/
public void testUnsizedDoublesCountSeq() {
LongAdder counter = new LongAdder();
ThreadLocalRandom r = ThreadLocalRandom.current();
long size = 100;
r.doubles().limit(size).forEach(x -> {
counter.increment();
});
assertEquals(counter.sum(), size);
}
示例9: randomMetric
import java.util.concurrent.ThreadLocalRandom; //导入方法依赖的package包/类
public static Metric randomMetric(int numMetrics, int numDimensions, int numLabels) {
ThreadLocalRandom r = ThreadLocalRandom.current();
int i = r.nextInt(0, numMetrics);
int[] labs = new int[numDimensions];
for (int j = 0; j < numDimensions; j++) {
labs[j] = r.nextInt(0, numLabels);
}
return metric(i, labs);
}
示例10: nextString
import java.util.concurrent.ThreadLocalRandom; //导入方法依赖的package包/类
/**
* Generates a random string of a length with a range, inclusive on either end.
* @param min min length
* @param max max length
* @return a random string whose length is with the provided boundaries (inclusive)
*/
public static String nextString(Integer min, Integer max) {
int length = stringLength(adjustStringLength(min), adjustStringLength(max));
StringBuilder sb = new StringBuilder(length);
Random random = ThreadLocalRandom.current();
for (int i = 0; i < length; i++) {
sb.append(RAND_CHARS.charAt(random.nextInt(RAND_CHARS.length())));
}
return sb.toString();
}
示例11: testLongsCount
import java.util.concurrent.ThreadLocalRandom; //导入方法依赖的package包/类
/**
* A parallel sized stream of longs generates the given number of values
*/
public void testLongsCount() {
LongAdder counter = new LongAdder();
ThreadLocalRandom r = ThreadLocalRandom.current();
long size = 0;
for (int reps = 0; reps < REPS; ++reps) {
counter.reset();
r.longs(size).parallel().forEach(x -> counter.increment());
assertEquals(size, counter.sum());
size += 524959;
}
}
示例12: setup
import java.util.concurrent.ThreadLocalRandom; //导入方法依赖的package包/类
public void setup() {
input = new GeoCoordTuple[size];
ThreadLocalRandom random = ThreadLocalRandom.current();
for (int i = 0; i < size; i++) {
input[i] = new GeoCoordTuple(
getLat(random),
getLong(random),
getLat(random),
getLong(random)
);
}
}
示例13: testUnsizedDoublesCountSeq
import java.util.concurrent.ThreadLocalRandom; //导入方法依赖的package包/类
/**
* A sequential unsized stream of doubles generates at least 100 values
*/
public void testUnsizedDoublesCountSeq() {
LongAdder counter = new LongAdder();
ThreadLocalRandom r = ThreadLocalRandom.current();
long size = 100;
r.doubles().limit(size).forEach(x -> counter.increment());
assertEquals(size, counter.sum());
}
示例14: testUnsizedLongsCountSeq
import java.util.concurrent.ThreadLocalRandom; //导入方法依赖的package包/类
/**
* A sequential unsized stream of longs generates at least 100 values
*/
public void testUnsizedLongsCountSeq() {
LongAdder counter = new LongAdder();
ThreadLocalRandom r = ThreadLocalRandom.current();
long size = 100;
r.longs().limit(size).forEach(x -> counter.increment());
assertEquals(size, counter.sum());
}
示例15: generator
import java.util.concurrent.ThreadLocalRandom; //导入方法依赖的package包/类
public static String generator(String dbName, String tblName, long beginTime, long endTime)
{
ConsumerConfig config = ConsumerConfig.INSTANCE();
String hdfsWarehouse = config.getHDFSWarehouse();
ThreadLocalRandom tlr = ThreadLocalRandom.current();
long random = tlr.nextLong();
return String.format("%s/%s/%s/%s-%s-%d%d%d", hdfsWarehouse, dbName, tblName, dbName, tblName, beginTime, endTime, random);
}