Debian Codegraph @

test/harness/jpype/proxy/ProxyExecutor.java

raw symbols SCIP
1 /* ****************************************************************************
2 Licensed under the Apache License, Version 2.0 (the "License");
3 you may not use this file except in compliance with the License.
4 You may obtain a copy of the License at
5
6 http://www.apache.org/licenses/LICENSE-2.0
7
8 Unless required by applicable law or agreed to in writing, software
9 distributed under the License is distributed on an "AS IS" BASIS,
10 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 See the License for the specific language governing permissions and
12 limitations under the License.
13
14 See NOTICE file for details.
15 **************************************************************************** */
16 package jpype.proxy;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.LinkedList;
22 import java.util.List;
23 import java.util.concurrent.Callable;
24 import java.util.concurrent.ExecutorService;
25 import java.util.concurrent.Executors;
26 import java.util.concurrent.Future;
27 import java.util.concurrent.TimeUnit;
28
29 public class ProxyExecutor
30 {
31
32 private ExecutorService executor;
33
34 private List<Future<Integer>> executedTasks = new ArrayList<Future<Integer>>();
35 private List<ProxyCaller> proxies = new ArrayList<ProxyCaller>();
36
37 public ProxyExecutor(int noOfThreads)
38 {
39 executor = Executors.newFixedThreadPool(noOfThreads);
40 }
41
42 public void shutdown()
43 {
44 executor.shutdown(); // Disable new tasks from being submitted
45 try
46 {
47 // Wait a while for existing tasks to terminate
48 if (!executor.awaitTermination(5, TimeUnit.SECONDS))
49 {
50 executor.shutdownNow(); // Cancel currently executing tasks
51 // Wait a while for tasks to respond to being cancelled
52 if (!executor.awaitTermination(5, TimeUnit.SECONDS))
53 System.err.println("Pool did not terminate");
54 }
55 } catch (InterruptedException ie)
56 {
57 // (Re-)Cancel if current thread also interrupted
58 executor.shutdownNow();
59 // Preserve interrupt status
60 Thread.currentThread().interrupt();
61 }
62 }
63
64 public void registerProxy(TestInterface4 proxy, int noOfExecutions)
65 {
66
67 for (int i = 0; i < noOfExecutions; i++)
68 {
69 proxies.add(new ProxyCaller(proxy));
70 }
71 }
72
73 public int getExpectedTasks()
74 {
75 return proxies.size();
76 }
77
78 public void runExecutor()
79 {
80
81 // Collections.shuffle(proxies);
82 for (ProxyCaller proxy : proxies)
83 {
84 Future<Integer> future = executor.submit(proxy);
85 executedTasks.add(future);
86 }
87
88 }
89
90 public int waitForExecutedTasks()
91 {
92 int returns = 0;
93
94 for (Future<Integer> task : executedTasks)
95 {
96 try
97 {
98 returns += task.get();
99 } catch (Exception ex)
100 {
101 System.out.println("An exception has thrown during execution");
102 ex.printStackTrace();
103 }
104 }
105 return returns;
106 }
107
108 public class ProxyCaller implements Callable<Integer>
109 {
110
111 private TestInterface4 proxy;
112
113 public ProxyCaller(TestInterface4 proxy)
114 {
115 this.proxy = proxy;
116 }
117
118 @Override
119 public Integer call() throws Exception
120 {
121
122 int result = 0;
123
124 try
125 {
126 int intValue = proxy.testMethodInt();
127 ReturnObject objectValue = proxy.testMethodObject();
128 String stringValue = proxy.testMethodString();
129 List<ReturnObject> listValue = proxy.testMethodList(5);
130 } catch (Exception ex)
131 {
132 ex.printStackTrace();
133 }
134
135 return 1; //result;
136 }
137
138 }
139 }