fork download
  1. import java.util.concurrent.atomic.AtomicInteger;
  2.  
  3. public class Main {
  4.  
  5. private static final long MAX_RUN_TIME = 5000l;
  6. private static final long ESTIMATED_OVERHEAD = 125l;
  7.  
  8. public static AtomicInteger totalCalculations = new AtomicInteger(0);
  9.  
  10. public static void main(String[] args) throws java.lang.Exception {
  11.  
  12. final long startTime = System.currentTimeMillis();
  13. final int NUM_CPU_CORES = Runtime.getRuntime().availableProcessors();
  14.  
  15. System.out.println("Starting on " + NUM_CPU_CORES + " threads.");
  16. final Thread[] threads = createThreads(NUM_CPU_CORES, new WorkPackage());
  17. startThreads(threads);
  18.  
  19. new Thread(new Runnable() {
  20. @Override
  21. public void run(){
  22. long currentTime = 0;
  23. long elapsedTime = 0;
  24. long remainingTime = MAX_RUN_TIME;
  25.  
  26. while(elapsedTime < (MAX_RUN_TIME - ESTIMATED_OVERHEAD)) {
  27. if(remainingTime > ESTIMATED_OVERHEAD) {
  28. try {
  29. Thread.sleep(remainingTime - ESTIMATED_OVERHEAD);
  30. } catch(InterruptedException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. currentTime = System.currentTimeMillis();
  35. elapsedTime = currentTime - startTime;
  36. remainingTime = MAX_RUN_TIME - elapsedTime;
  37. }
  38.  
  39. stopThreads(threads);
  40. System.out.println("Completed " + totalCalculations.intValue() + " useless calculations over "
  41. + (currentTime-startTime)/1000f + " seconds!");
  42. System.exit(0);
  43. Thread.currentThread().interrupt();
  44. }
  45. }).start();
  46. }
  47.  
  48. private static Thread[] createThreads(final int numCpuCores, final Runnable runnable) {
  49. Thread[] workerThreads = new Thread[numCpuCores];
  50. for(int i=0; i<numCpuCores; i++) {
  51. workerThreads[i] = new Thread(runnable);
  52. }
  53. return workerThreads;
  54. }
  55.  
  56. private static void startThreads(final Thread[] threadsToRun) {
  57. for(Thread thread : threadsToRun) {
  58. thread.start();
  59. }
  60. }
  61.  
  62. private static void stopThreads(final Thread[] threadsToStop) {
  63. for(Thread thread : threadsToStop) {
  64. thread.interrupt();
  65. }
  66. }
  67. }
  68.  
  69. class WorkPackage implements Runnable {
  70. @Override
  71. public void run() {
  72. while(!Thread.interrupted()) {
  73. int result = xorShiftRandom(Integer.MAX_VALUE) + xorShiftRandom(Integer.MAX_VALUE);
  74. Main.totalCalculations.incrementAndGet();
  75. }
  76. }
  77.  
  78. private long currentValue = System.currentTimeMillis();
  79. private int xorShiftRandom(final int max) {
  80. currentValue ^= (currentValue << 21);
  81. currentValue ^= (currentValue >>> 35);
  82. currentValue ^= (currentValue << 4);
  83. int out = (int) currentValue % max;
  84. return (out < 0) ? -out : out;
  85. }
  86. }
Success #stdin #stdout 4.98s 381824KB
stdin
Standard input is empty
stdout
Starting on 4 threads.
Completed 32619031 useless calculations over 4.919 seconds!