1 18 package com.Ostermiller.util; 19 20 import java.util.*; 21 22 30 class ParallelizerTests { 31 public static void main (String [] args){ 32 try { 33 final HashMap<String ,Date> results = new HashMap<String ,Date>(); 34 final Random random = new Random(); 35 Parallelizer pll = new Parallelizer(8); 36 for(int i=0; i<100; i++){ 37 final String hashKey = Integer.toString(i); 38 pll.run( 39 new Runnable (){ 40 public void run(){ 41 try { 42 Thread.sleep(random.nextInt(5000)); 43 results.put(hashKey,new Date()); 44 } catch (RuntimeException rx){ 45 throw rx; 46 } catch (Exception x){ 47 throw new RuntimeException (x); 48 } 49 } 50 } 51 ); 52 } 53 if (results.size() == 100) throw new Exception ("Expected results to not yet have 100 items in it."); 54 pll.join(); 55 if (results.size() != 100) throw new Exception ("Expected results to have 100 items, not " + results.size()); 56 for(int i=0; i<100; i++){ 57 String hashKey = Integer.toString(i); 58 Date result = (Date)results.get(hashKey); 59 if (result == null) throw new Exception (hashKey + " not in map"); 60 } 61 System.exit(0); 62 63 pll = new Parallelizer(); 64 pll.run( 65 new Runnable (){ 66 public void run(){ 67 throw new RuntimeException ("Testing Parallelizer"); 68 } 69 } 70 ); 71 72 try { 73 pll.join(); 74 throw new Exception ("Parallelizer appears not to have thrown expected exception"); 75 } catch (RuntimeException rtx){ 76 if (!"Testing Parallelizer".equals(rtx.getMessage())){ 77 throw new Exception ("Expected Testing Parallelizer as message to exception"); 78 } 79 } 80 81 } catch (Throwable x){ 82 x.printStackTrace(System.err); 83 System.exit(1); 84 } 85 } 86 } 87 | Popular Tags |