View Javadoc
1   //******************************************************************************
2   //
3   // File:    MultipleParallelException.java
4   // Package: edu.rit.pj
5   // Unit:    Class edu.rit.pj.MultipleParallelException
6   //
7   // This Java source file is copyright (C) 2007 by Alan Kaminsky. All rights
8   // reserved. For further information, contact the author, Alan Kaminsky, at
9   // ark@cs.rit.edu.
10  //
11  // This Java source file is part of the Parallel Java Library ("PJ"). PJ is free
12  // software; you can redistribute it and/or modify it under the terms of the GNU
13  // General Public License as published by the Free Software Foundation; either
14  // version 3 of the License, or (at your option) any later version.
15  //
16  // PJ is distributed in the hope that it will be useful, but WITHOUT ANY
17  // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18  // A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19  //
20  // Linking this library statically or dynamically with other modules is making a
21  // combined work based on this library. Thus, the terms and conditions of the GNU
22  // General Public License cover the whole combination.
23  //
24  // As a special exception, the copyright holders of this library give you
25  // permission to link this library with independent modules to produce an
26  // executable, regardless of the license terms of these independent modules, and
27  // to copy and distribute the resulting executable under terms of your choice,
28  // provided that you also meet, for each linked independent module, the terms
29  // and conditions of the license of that module. An independent module is a module
30  // which is not derived from or based on this library. If you modify this library,
31  // you may extend this exception to your version of the library, but you are not
32  // obligated to do so. If you do not wish to do so, delete this exception
33  // statement from your version.
34  //
35  // A copy of the GNU General Public License is provided in the file gpl.txt. You
36  // may also obtain a copy of the GNU General Public License on the World Wide
37  // Web at http://www.gnu.org/licenses/gpl.html.
38  //
39  //******************************************************************************
40  package edu.rit.pj;
41  
42  import java.io.*;
43  import java.util.Map;
44  import java.util.concurrent.ConcurrentHashMap;
45  
46  /**
47   * Class MultipleParallelException is thrown to indicate that multiple threads
48   * in a parallel team threw exceptions while executing a parallel construct. The
49   * individual exceptions thrown by the threads are contained in a {@linkplain
50   * java.util.Map Map} mapping the thread index (type Integer) to the exception
51   * thrown by that thread (type Throwable).
52   *
53   * @author Alan Kaminsky
54   * @version 16-May-2007
55   */
56  public class MultipleParallelException
57          extends Exception {
58  
59      @Serial
60      private static final long serialVersionUID = 1L;
61  
62  // Hidden data members.
63      private ConcurrentHashMap<Integer, Throwable> myMap;
64  
65  // Exported constructors.
66      /**
67       * Create a new multiple parallel exception with no detail message and no
68       * exception map.
69       */
70      public MultipleParallelException() {
71          super();
72      }
73  
74      /**
75       * Create a new multiple parallel exception with the given detail message
76       * and no exception map.
77       *
78       * @param theMessage Detail message.
79       */
80      public MultipleParallelException(String theMessage) {
81          super(theMessage);
82      }
83  
84      /**
85       * Create a new multiple parallel exception with no detail message and the
86       * given exception map.
87       *
88       * @param theMap Exception map.
89       */
90      public MultipleParallelException(ConcurrentHashMap<Integer, Throwable> theMap) {
91          super();
92          myMap = theMap;
93      }
94  
95      /**
96       * Create a new multiple parallel exception with the given detail message
97       * and the given exception map.
98       *
99       * @param theMessage Detail message.
100      * @param theMap Exception map.
101      */
102     public MultipleParallelException(String theMessage, ConcurrentHashMap<Integer, Throwable> theMap) {
103         super(theMessage);
104         myMap = theMap;
105     }
106 
107 // Exported operations.
108     /**
109      * Obtain this multiple parallel exception's exception map.
110      *
111      * @return Exception map, or null if none.
112      */
113     public Map<Integer, Throwable> getExceptionMap() {
114         return myMap;
115     }
116 
117     /**
118      * {@inheritDoc}
119      *
120      * Print this throwable and its backtrace to the specified print stream. The
121      * stack traces of this multiple parallel exception itself and of all the
122      * wrapped exceptions in the exception map are printed.
123      */
124     public void printStackTrace(PrintStream s) {
125         synchronized (s) {
126             super.printStackTrace(s);
127             if (myMap != null) {
128                 for (Map.Entry<Integer, Throwable> entry : myMap.entrySet()) {
129                     s.println("Parallel team thread " + entry.getKey() + ":");
130                     entry.getValue().printStackTrace(s);
131                 }
132             }
133         }
134     }
135 
136     /**
137      * Print this throwable and its backtrace to the specified print writer. The
138      * stack traces of this multiple parallel exception itself and of all the
139      * wrapped exceptions in the exception map are printed.
140      *
141      * @param s Print writer to use for output.
142      */
143     public void printStackTrace(PrintWriter s) {
144         synchronized (s) {
145             super.printStackTrace(s);
146             if (myMap != null) {
147                 for (Map.Entry<Integer, Throwable> entry : myMap.entrySet()) {
148                     s.println("Parallel team thread " + entry.getKey() + ":");
149                     entry.getValue().printStackTrace(s);
150                 }
151             }
152         }
153     }
154 
155 }