Long Lasting Java SDK Testing Methods

I am working on some tutorials and sample java.util.concurrent package. Typically, authors of an example place a placeholder marked with the comment "long-running task." Since these examples relate to parallel programming, I don't really like to use Thread.sleep (long), surrounded by try-catch blocks.

What do you use in these circumstances?

To open the URL, do some complicated math with floating point, i / o ... Optimally, these lengthy tasks have no side effects.

These methods can be thought of as Loren Ipsums on a timeline.


I will add specific implementations here:

import java.math.BigInteger; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.SecureRandom; import java.util.Random; public class LongRunningTasks { public void triggerKeyGeneration(int iterations) { try { long start = System.currentTimeMillis(); for (int i = 0; i < iterations; i++) { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", "SUN"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN"); keyGen.initialize(1024, random); keyGen.generateKeyPair(); } System.out.println("triggerKeyGeneration: " + (System.currentTimeMillis() - start)); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchProviderException e) { e.printStackTrace(); } } private static final int SCALE = 10000; private static final int ARRINIT = 2000; /** * http://www.codecodex.com/wiki/index.php?title=Digits_of_pi_calculation#Java * * @param digits - returns good results up to 12500 digits * @return */ public String piDigits(int digits){ StringBuffer pi = new StringBuffer(); int[] arr = new int[digits + 1]; int carry = 0; for (int i = 0; i <= digits; ++i) arr[i] = ARRINIT; for (int i = digits; i > 0; i-= 14) { int sum = 0; for (int j = i; j > 0; --j) { sum = sum * j + SCALE * arr[j]; arr[j] = sum % (j * 2 - 1); sum /= j * 2 - 1; } pi.append(String.format("%04d", carry + sum / SCALE)); carry = sum % SCALE; } return pi.toString(); } private static final Random rand = new Random(); private static final BigInteger veryBig = new BigInteger(1200, rand); public BigInteger nextProbablePrime() { return veryBig.nextProbablePrime(); } } 
+4
source share
6 answers

I once used a public key generator to do something like this. This is an intensive processor task. Doing this hundreds or thousands of times should give you a significant delay.

+3
source
 BigInteger veryBig = new BigInteger(10000, new Random()); veryBig.nextProbablePrime(); 
+4
source

Count pi on a very large number of digits. A quick google picked up this implementation , which should do the trick :)

+1
source

I use I / O (both local and non-local), and often long work has a side effect.

0
source

If we talk about methods that in any case require full activity, how about a simple loop running for a certain time?

 public void triggerKeyGeneration(int nbSec) { long objectiveTime = System.currentTimeMillis() + nbSec*1000; while (System.currentTimeMillis() < objectiveTime); } 
0
source

Here are some issues:

0
source

Source: https://habr.com/ru/post/1286619/


All Articles