Long running methods from Java SDK for testing purposes


Long running methods from Java SDK for testing purposes



I'm working through some tutorials and examples of java.util.concurrent package. Usually example authors put a placeholder marked with a comment 'long running task'. Since these examples are about concurrent programming I'm not keen of using Thread.sleep(long), surrounded by try-catch blocks.

What do you use in these circumstances?

To open an url, do some complicated float math, i/o... Optimally these long running tasks do not have any side effects.

These methods can be seen as Loren Ipsums on the timescale.


I'll add here concrete implementations:

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();     } } 



How to find common strings among two very large files?

1:



XML Versioning Algorithm
I've once used a public-private key generator to do something similar.


Keeping top 5 values in PHP efficently
This is a CPU intensive task.


Optimize Binary Search Algorithm
Doing it hundreds or thousands of times should give you a considerable delay..
Finding Strings Neighbors By Up To 2 Differing Positions


Ads System


How can I search a graph for a path?

2:



What data structure should I use to keep track of recently used items?
Calculate pi to a very large number of digits.

A quick google brought up this implementation which should do the trick :).


3:


BigInteger veryBig = new BigInteger(10000, new Random()); veryBig.nextProbablePrime(); 


4:


I use I/O (both local and non), and often a long running task have a side effect..


5:


If talking about methods which will anyway require a full activity, how about a simple loop running for a specified time ?.
public void triggerKeyGeneration(int nbSec) {     long objectiveTime = System.currentTimeMillis() + nbSec*1000;     while (System.currentTimeMillis() < objectiveTime); } 


6:


Here some problems:.



85 out of 100 based on 45 user ratings 895 reviews