Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 8th, 2012  |  syntax: None  |  size: 2.41 KB  |  hits: 6  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Why doesn't it use all of the processor?
  2. import javax.swing.JTextPane;
  3.  
  4. public class Main {
  5.  
  6.     static int numberToCalculate = 100;
  7.     static int otherNumberToCalculate = 50;
  8.     static String typeOfCalculation = "all";
  9.     static int calculated;
  10.     static int calculations = 10000000;
  11.  
  12.     public static void calculate(JTextPane j, JTextPane j2) {
  13.         long time = System.currentTimeMillis();
  14.         for(int i = 0; i <= calculations; i++) {
  15.  
  16.             switch(typeOfCalculation) {
  17.             case "Divide":
  18.                 calculated = numberToCalculate / otherNumberToCalculate;
  19.                 break;
  20.  
  21.             case "Multiply":
  22.                 calculated = numberToCalculate * otherNumberToCalculate;
  23.                 break;
  24.  
  25.             case "Plus":
  26.                 calculated = numberToCalculate + otherNumberToCalculate;
  27.                 break;
  28.  
  29.             case "Minus":
  30.                 calculated = numberToCalculate - otherNumberToCalculate;
  31.                 break;
  32.  
  33.             case "All":
  34.                 calculated = numberToCalculate / otherNumberToCalculate;
  35.                 calculated = calculated * otherNumberToCalculate;
  36.                 calculated = calculated + otherNumberToCalculate;
  37.                 calculated = calculated - otherNumberToCalculate;
  38.                 break;
  39.  
  40.             default:
  41.                 Test.UpdateText(j, "Error, please pick type of calculation.");
  42.                 Test.UpdateText(j2, "Error, please pick type of calculation.");
  43.                 break;
  44.             }
  45.             if(i == calculations) {
  46.                 Test.UpdateText(j, "Milliseconds: " + (System.currentTimeMillis() - time));
  47.                 Test.UpdateText(j2, "Result: " + calculated);
  48.             }
  49.         }
  50.     }
  51.  
  52.     public static void main(String [] args)
  53.     {
  54.         Test.window();
  55.     }
  56.  
  57. }
  58.        
  59. public static void main(String[] args) {
  60.     ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
  61.  
  62.     Runnable r = new Runnable() {
  63.  
  64.         @Override
  65.         public void run() {
  66.             double sum = 0;
  67.             for (int i = 1; i < 100000000; i++) {
  68.                 sum += Math.log(i);
  69.             }
  70.             System.out.println(sum);
  71.         }
  72.     };
  73.  
  74.     r.run(); //first run: single thread
  75.  
  76.     //second run: as many threads as processors
  77.     for (int i = 0; i < Runtime.getRuntime().availableProcessors(); i++) {
  78.         executor.submit(r);
  79.     }
  80.  
  81.     executor.shutdown();
  82. }