KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Ostermiller > util > ParallelizerTests


1 /*
2  * Parallelizer tests
3  * Copyright (C) 2005 Stephen Ostermiller
4  * http://ostermiller.org/contact.pl?regarding=Java+Utilities
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * See COPYING.TXT for details.
17  */

18 package com.Ostermiller.util;
19
20 import java.util.*;
21
22 /**
23  * Regression test for Parallelizer.
24  * More information about this class is available from <a target="_top" HREF=
25  * "http://ostermiller.org/utils/Parallelizer.html">ostermiller.org</a>.
26  *
27  * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
28  * @since ostermillerutils 1.04.00
29  */

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