KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > io > JSE5Executor


1 /*
2  * JSE5Executor.java
3  *
4  * Created on 18. Dezember 2006, 01:48
5  */

6 /*
7  * Copyright 2006 Schlichtherle IT Services
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package de.schlichtherle.io;
23
24 /**
25  * @author Christian Schlichtherle
26  * @version @version@
27  * @since TrueZIP 6.4
28  */

29 final class JSE5Executor implements File.Executor {
30     private final java.util.concurrent.ExecutorService JavaDoc service;
31
32     /**
33      * Constructs a new <code>JSE5Executor</code>.
34      * This constructor is public in order to enable reflective access!
35      */

36     public JSE5Executor(final String JavaDoc threadName) {
37         assert threadName != null;
38         service = java.util.concurrent.Executors.newCachedThreadPool(
39                 new java.util.concurrent.ThreadFactory JavaDoc() {
40             public Thread JavaDoc newThread(final Runnable JavaDoc r) {
41                 final Thread JavaDoc t = new Thread JavaDoc(r, threadName);
42                 t.setDaemon(true);
43                 return t;
44             }
45         });
46     }
47
48     public File.Task submit(Runnable JavaDoc target) {
49         assert target != null;
50         return new JSE5Task(service.submit(target));
51     }
52
53     private static final class JSE5Task implements File.Task {
54         private final java.util.concurrent.Future JavaDoc future;
55
56         private JSE5Task(java.util.concurrent.Future JavaDoc future) {
57             assert future != null;
58             this.future = future;
59         }
60
61         public void cancel() {
62             future.cancel(true);
63             while (true) {
64                 try {
65                     future.get();
66                     break;
67                 } catch (java.util.concurrent.CancellationException JavaDoc cancelled) {
68                     break;
69                 } catch (java.util.concurrent.ExecutionException JavaDoc readerFailure) {
70                     assert false : readerFailure;
71                     break;
72                 } catch (InterruptedException JavaDoc ignored) {
73                 }
74             }
75         }
76     }
77 }
78
Popular Tags