[cvs] / jsr166 / etc / notes / tim-executor-examples.html Repository:
ViewVC logotype

Diff of /jsr166/etc/notes/tim-executor-examples.html

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.5, Mon Jan 20 19:28:14 2003 UTC revision 1.6, Wed Jan 22 05:50:38 2003 UTC
# Line 62  Line 62 
62                      return blockingService.serve(req);                      return blockingService.serve(req);
63                  }                  }
64              };              };
65              FutureTask<Response> ftask = new FutureTask<Response>(task);              return Executors.execute(executor, task);
             executor.execute(ftask);  
             return ftask;  
66          }          }
67    
68          private final BlockingService blockingService;          private final BlockingService blockingService;
# Line 134  Line 132 
132          public SingleThreadAdapter (String library) {          public SingleThreadAdapter (String library) {
133              this.executor = Executors.newSingleThreadExecutor();              this.executor = Executors.newSingleThreadExecutor();
134              try {              try {
135                  FutureTask ftask = new FutureTask(new Runnable () {                  Executors.invoke(executor, new Runnable() {
136                      public void run () {                      public void run () { System.loadLibrary(library); }
137                          System.loadLibrary(library);                  }, Boolean.TRUE);
138                      }              } catch (ExecutionException e) {
139                  });                  // System.loadLibrary does not throw checked exceptions.
                 executor.execute(ftask);  
                 ftask.get();  
             }  
             catch (ExecutionException e) {  
                 if (e.getCause() == null) {  
                     throw new RuntimeException(e);  
                 }  
140                  if (e.getCause() instanceof RuntimeException) {                  if (e.getCause() instanceof RuntimeException) {
141                      throw (RuntimeException) e.getCause();                      throw (RuntimeException) e.getCause();
142                  }                  }
                 if (e.getCause() instanceof Error) {  
143                      throw (Error) e.getCause();                      throw (Error) e.getCause();
144                  }                  }
                 throw new RuntimeException(e.getCause());  
             }  
145              // ... other invocation or interruption related catch clauses ...              // ... other invocation or interruption related catch clauses ...
146          }          }
147    
148          public Object newInstance (Callable creator) throws Exception {          public Object newInstance (Callable creator) throws Exception {
149              try {              try {
150                  FutureTask createTask = new FutureTask(creator);                  Object instance = Executors.invoke(executor, creator);
                 executor.execute(createTask);  
                 Object instance = createTask.get();  
151                  return Proxy.newProxyInstance(                  return Proxy.newProxyInstance(
152                      instance.getClass().getClassLoader(),                      instance.getClass().getClassLoader(),
153                      instance.getClass().getInterfaces(),                      instance.getClass().getInterfaces(),
154                      new Handler(instance));                      new Handler(instance));
155              }              } catch (ExecutionException e) {
             catch (ExecutionException e) {  
                 if (e.getCause() == null) {  
                     throw e;  
                 }  
156                  if (e.getCause() instanceof Exception) {                  if (e.getCause() instanceof Exception) {
157                      throw e.getCause();                      throw (Exception) e.getCause();
                 }  
                 else {  
                     throw new RuntimeException(e.getCause());  
158                  }                  }
159                    throw (Error) e.getCause();
160              }              }
161              // ... other invocation or interruption related catch clauses ...              // ... other invocation or interruption related catch clauses ...
162          }          }
# Line 195  Line 175 
175                                  throw e;                                  throw e;
176                              }                              }
177                              catch (Throwable e) {                              catch (Throwable e) {
178                                  throw RuntimeException(e);                                  throw (Error) e;
179                              }                              }
180                          }                          }
181                      };                      };
182                      FutureTask ftask = new FutureTask(task)                      return Executors.invoke(executor, task);
183                      executor.execute(ftask);                  } catch (ExecutionException e) {
184                      return ftask.get();                      throw e.getCause();
                 }  
                 catch (ExecutionException e) {  
                     throw e.getCause() == null ? e : e.getCause();  
185                  }                  }
186                  // ... other invocation or interruption related catch clauses ...                  // ... other invocation or interruption related catch clauses ...
187              }              }
# Line 241  Line 218 
218  of Executor will be used in the implementation, there is no reason to  of Executor will be used in the implementation, there is no reason to
219  expose this anywhere except in the SingleThreadAdapter constructor. </p>  expose this anywhere except in the SingleThreadAdapter constructor. </p>
220    
221  <p> The exception handling is delicate and long winded. I punted on  <p> The exception handling is a bit tricky. Joe Bowbeer showed me
222  most of it, and I'm not sure I got the rest right either. </p>  how to do it right, but it took me a while to understand <em>why</em>
223    it was right. I wonder if other users would be similarly confused. </p>
224    
225  </body>  </body>
226  </html>  </html>

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.6

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8