List of usage examples for java.util.concurrent ThreadLocalRandom current
public static ThreadLocalRandom current()
From source file:org.talend.components.salesforce.SalesforceTestBase.java
public String createNewRandom() { return Integer.toString(ThreadLocalRandom.current().nextInt(1, 100000)); }
From source file:alluxio.multi.process.MultiProcessCluster.java
private MultiProcessCluster(Map<PropertyKey, String> properties, int numMasters, int numWorkers, String clusterName, DeployMode mode) { mProperties = properties;//ww w . j a v a 2s . c om mNumMasters = numMasters; mNumWorkers = numWorkers; // Add a unique number so that different runs of the same test use different cluster names. mClusterName = clusterName + ThreadLocalRandom.current().nextLong(); mDeployMode = mode; mMasters = new ArrayList<>(); mWorkers = new ArrayList<>(); mCloser = Closer.create(); mState = State.NOT_STARTED; mSuccess = false; }
From source file:com.abhinavjhanwar.android.egg.neko.Cat.java
public Cat(Context context, long seed, List<Cat> mCats) { D = new CatParts(context); mSeed = seed;/* w w w. j a v a 2 s.c om*/ long check = seed; for (int i = 0; i < mCats.size(); i++) { while (context.getString(R.string.default_cat_name, String.valueOf(check).substring(0, 3)) .equals(mCats.get(i).getName())) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { check = Math.abs(ThreadLocalRandom.current().nextInt()); } else { check = Math.abs(new Random().nextInt()); } } } setName(context.getString(R.string.default_cat_name, String.valueOf(check).substring(0, 3))); final Random nsr = notSoRandom(seed); // body color mBodyColor = chooseP(nsr, P_BODY_COLORS); if (mBodyColor == 0) mBodyColor = Color.HSVToColor(new float[] { nsr.nextFloat() * 360f, frandrange(nsr), frandrange(nsr) }); tint(mBodyColor, D.body, D.head, D.leg1, D.leg2, D.leg3, D.leg4, D.tail, D.leftEar, D.rightEar, D.foot1, D.foot2, D.foot3, D.foot4, D.tailCap); tint(0x20000000, D.leg2Shadow, D.tailShadow); if (isDark(mBodyColor)) { tint(0xFFFFFFFF, D.leftEye, D.rightEye, D.mouth, D.nose); } tint(isDark(mBodyColor) ? 0xFFEF9A9A : 0x20D50000, D.leftEarInside, D.rightEarInside); tint(chooseP(nsr, P_BELLY_COLORS), D.belly); tint(chooseP(nsr, P_BELLY_COLORS), D.back); final int faceColor = chooseP(nsr, P_BELLY_COLORS); tint(faceColor, D.faceSpot); if (!isDark(faceColor)) { tint(0xFF000000, D.mouth, D.nose); } if (nsr.nextFloat() < 0.25f) { tint(0xFFFFFFFF, D.foot1, D.foot2, D.foot3, D.foot4); } else { if (nsr.nextFloat() < 0.25f) { tint(0xFFFFFFFF, D.foot1, D.foot2); } else if (nsr.nextFloat() < 0.25f) { tint(0xFFFFFFFF, D.foot3, D.foot4); } else if (nsr.nextFloat() < 0.1f) { tint(0xFFFFFFFF, (Drawable) choose(nsr, D.foot1, D.foot2, D.foot3, D.foot4)); } } tint(nsr.nextFloat() < 0.333f ? 0xFFFFFFFF : mBodyColor, D.tailCap); final int capColor = chooseP(nsr, isDark(mBodyColor) ? P_LIGHT_SPOT_COLORS : P_DARK_SPOT_COLORS); tint(capColor, D.cap); //tint(chooseP(nsr, isDark(bodyColor) ? P_LIGHT_SPOT_COLORS : P_DARK_SPOT_COLORS), D.nose); final int collarColor = chooseP(nsr, P_COLLAR_COLORS); tint(collarColor, D.collar); tint((nsr.nextFloat() < 0.1f) ? collarColor : 0, D.bowtie); }
From source file:frequencyanalysis.FrequencyAnalysis.java
public static String generateRandom25LetterString() { String alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String rand = ""; while (rand.length() < 25) { int randInt = ThreadLocalRandom.current().nextInt(0, 25 - rand.length()); rand = rand + alphabets.charAt(randInt); alphabets = alphabets.replace(String.valueOf(alphabets.charAt(randInt)), ""); }//from ww w . j a va2s .co m return rand; }
From source file:org.apache.bookkeeper.common.conf.ConfigKeyTest.java
@Test public void testGetInt() { String keyName = runtime.getMethodName(); int defaultValue = ThreadLocalRandom.current().nextInt(10000); ConfigKey key = ConfigKey.builder(keyName).required(true).type(Type.INT).defaultValue(defaultValue).build(); Configuration conf = new ConcurrentConfiguration(); // get default value assertEquals(defaultValue, key.getInt(conf)); assertEquals(defaultValue, key.get(conf)); // set value/* w ww. j a v a2 s . c o m*/ int newValue = defaultValue * 2; key.set(conf, newValue); assertEquals(newValue, key.getInt(conf)); assertEquals(newValue, key.get(conf)); }
From source file:org.illalabs.rss.RssStreamProviderTask.java
@Override public void run() { boolean work = true; int retries = 0; try {//from w ww . ja v a 2 s . co m while (work) { feedDetails = this.rssQueue.poll(); //If there is nothing, then wait for some time if (retries < MAX_RETRIES && this.feedDetails == null) { this.waiting(DEFAULT_TIME_OUT / (ThreadLocalRandom.current().nextInt(MAX_RETRIES) + 1)); retries++; } else if (this.feedDetails != null) { // if enough time has passed, then read again long waitTime = (System.currentTimeMillis() - this.feedDetails.getLastPolled()) / 1000; if (waitTime > this.feedDetails.getPollIntervalMillis()) { Set<String> batch = queueFeedEntries(new URL(feedDetails.getUrl())); // do something with batch System.out.println( "Batch " + feedDetails.getUrl() + " " + this.feedDetails.getLastPolled() + ""); PREVIOUSLY_SEEN.put(feedDetails.getUrl(), batch); work = false; this.feedDetails.setLastPolled(System.currentTimeMillis()); } else { LOGGER.info(this.feedDetails.getUrl() + " has been already polled."); this.waiting(waitTime); } // Put back the item we just worked with this.rssQueue.put(this.feedDetails); } else { // if we waited, and there is nothing to work on work = false; } } LOGGER.info("Worker finished"); } catch (IOException | FeedException | InterruptedException e) { LOGGER.warn("Exception while reading rss stream, {} : {}", feedDetails, e); } }
From source file:org.apache.bookkeeper.tools.perf.dlog.PerfWriter.java
PerfWriter(ServiceURI serviceURI, Flags flags) { this.serviceURI = serviceURI; this.flags = flags; this.payload = new byte[flags.recordSize]; ThreadLocalRandom.current().nextBytes(payload); }
From source file:org.neo4j.kernel.api.impl.index.storage.PartitionedIndexStorageTest.java
private void createRandomFilesAndFolders(File rootFolder) throws IOException { int count = ThreadLocalRandom.current().nextInt(10) + 1; for (int i = 0; i < count; i++) { if (ThreadLocalRandom.current().nextBoolean()) { createRandomFile(rootFolder); } else {// w w w . j av a2s .c o m createRandomFolder(rootFolder); } } }
From source file:com.xpn.xwiki.internal.objects.classes.UsedValuesListQueryBuilder.java
private boolean canView(ListClass listClass, String value, long count) { // We can't check all the documents where this value occurs so we check just one of them, chosen randomly. long offset = ThreadLocalRandom.current().nextLong(count); String statement = String.format( "select obj.name from BaseObject as obj, %2$s " + "where obj.className = :className and obj.name <> :templateName " + "and prop.id.id = obj.id and prop.id.name = :propertyName and %1$s = :propertyValue", getSelectColumnAndFromTable(listClass)); try {/*from w ww .ja v a 2 s . co m*/ Query query = this.queryManager.createQuery(statement, Query.HQL); bindParameterValues(query, listClass); query.bindValue("propertyValue", value); query.setWiki(listClass.getReference().extractReference(EntityType.WIKI).getName()); query.setOffset((int) offset).setLimit(1); List<?> results = query.execute(); if (results.size() > 0) { DocumentReference documentReference = this.documentReferenceResolver .resolve((String) results.get(0)); if (this.authorization.hasAccess(Right.VIEW, documentReference)) { return true; } } } catch (QueryException e) { this.logger.warn( "Failed to check if the list value is viewable. Root cause is [{}]." + " Continue assuming the value is not viewable.", ExceptionUtils.getRootCauseMessage(e)); } return false; }
From source file:frequencyanalysis.FrequencyAnalysis.java
public static String modifyString(String input, int maxMode) { int randMode = ThreadLocalRandom.current().nextInt(0, 3); String ans = ""; if (randMode == 0) { // Replace any random 2 letters int randInt1 = ThreadLocalRandom.current().nextInt(0, 25); int randInt2 = ThreadLocalRandom.current().nextInt(0, 25); ans = input.replace(String.valueOf(input.charAt(randInt1)), "*") .replace(String.valueOf(input.charAt(randInt2)), String.valueOf(input.charAt(randInt1))) .replace("*", String.valueOf(input.charAt(randInt2))); } else if (randMode == 1) { // Swap any 2 random rows int randInt3 = ThreadLocalRandom.current().nextInt(0, 5); int randInt4 = ThreadLocalRandom.current().nextInt(0, 5); ans = input.replace(input.substring(randInt3 * 5, randInt3 * 5 + 5), "*****") .replace(input.substring(randInt4 * 5, randInt4 * 5 + 5), input.substring(randInt3 * 5, randInt3 * 5 + 5)) .replace("*****", input.substring(randInt4 * 5, randInt4 * 5 + 5)); } else if (randMode == 2) { //Swap any 2 random columns int randInt5 = ThreadLocalRandom.current().nextInt(0, 5); int randInt6 = ThreadLocalRandom.current().nextInt(0, 5); ans = input.replace(String.valueOf(input.charAt(randInt5)), "*") .replace(String.valueOf(input.charAt(randInt6)), String.valueOf(input.charAt(randInt5))) .replace("*", String.valueOf(input.charAt(randInt6))) .replace(String.valueOf(input.charAt(randInt5 + 5)), "*") .replace(String.valueOf(input.charAt(randInt6 + 5)), String.valueOf(input.charAt(randInt5 + 5))) .replace("*", String.valueOf(input.charAt(randInt6 + 5))) .replace(String.valueOf(input.charAt(randInt5 + 10)), "*") .replace(String.valueOf(input.charAt(randInt6 + 10)), String.valueOf(input.charAt(randInt5 + 10))) .replace("*", String.valueOf(input.charAt(randInt6 + 10))) .replace(String.valueOf(input.charAt(randInt5 + 15)), "*") .replace(String.valueOf(input.charAt(randInt6 + 15)), String.valueOf(input.charAt(randInt5 + 15))) .replace("*", String.valueOf(input.charAt(randInt6 + 15))) .replace(String.valueOf(input.charAt(randInt5 + 20)), "*") .replace(String.valueOf(input.charAt(randInt6 + 20)), String.valueOf(input.charAt(randInt5 + 20))) .replace("*", String.valueOf(input.charAt(randInt6 + 20))); }//from w ww .j a va2s . com return ans; }