|
10 | 10 | import java.math.BigInteger; |
11 | 11 | import java.util.*; |
12 | 12 | import java.io.BufferedReader; |
| 13 | +import java.util.concurrent.*; |
| 14 | +import java.util.function.Consumer; |
13 | 15 | import java.util.function.Function; |
14 | 16 | import java.util.stream.Collectors; |
15 | 17 |
|
@@ -2237,4 +2239,158 @@ private static final String indent(int indent) { |
2237 | 2239 | } |
2238 | 2240 | return sb.toString(); |
2239 | 2241 | } |
| 2242 | + |
| 2243 | +// public static class asyncRunner { |
| 2244 | +// private List<Future<JSONObject>> tasks; |
| 2245 | +// private boolean running; |
| 2246 | +// |
| 2247 | +// public asyncRunner() { |
| 2248 | +// this.tasks = new ArrayList<>(); |
| 2249 | +// running = false; |
| 2250 | +// } |
| 2251 | +// |
| 2252 | +// public void add(Future<JSONObject> task) { |
| 2253 | +// this.tasks.add(task); |
| 2254 | +// } |
| 2255 | +// |
| 2256 | +// |
| 2257 | +// public void run() { |
| 2258 | +// running = true; |
| 2259 | +// while(running) { |
| 2260 | +// List<Future<JSONObject>> nextTasks = new ArrayList<>(); |
| 2261 | +// for(Future<JSONObject> task: tasks) { |
| 2262 | +// if(!task.isDone()) { |
| 2263 | +// nextTasks.add(task); |
| 2264 | +// } |
| 2265 | +// } |
| 2266 | +// running = !nextTasks.isEmpty(); |
| 2267 | +// tasks = nextTasks; |
| 2268 | +// } |
| 2269 | +// } |
| 2270 | +// } |
| 2271 | +// |
| 2272 | +// public static Future<JSONObject> toJSONObject(Reader reader, Consumer<JSONObject> after, Consumer<Exception> error){ |
| 2273 | +// |
| 2274 | +// ExecutorService executor = Executors.newSingleThreadExecutor(); |
| 2275 | +// futureTask task = new futureTask(reader, after, error); |
| 2276 | +// Future<JSONObject> future = executor.submit(task); |
| 2277 | +// |
| 2278 | +// return future; |
| 2279 | +// } |
| 2280 | +// |
| 2281 | +// private static class futureTask implements Callable<JSONObject> { |
| 2282 | +// |
| 2283 | +// Reader reader; |
| 2284 | +// Consumer<JSONObject> after; |
| 2285 | +// Consumer<Exception> error; |
| 2286 | +// public futureTask(Reader reader, Consumer<JSONObject> after, Consumer<Exception> error) { |
| 2287 | +// this.reader = reader; |
| 2288 | +// this.after = after; |
| 2289 | +// this.error = error; |
| 2290 | +// } |
| 2291 | +// |
| 2292 | +// @Override |
| 2293 | +// public JSONObject call() throws Exception { |
| 2294 | +// JSONObject jo = new JSONObject(); |
| 2295 | +// try { |
| 2296 | +// XMLTokener x = new XMLTokener(reader); |
| 2297 | +// while (x.more()) { |
| 2298 | +// x.skipPast("<"); |
| 2299 | +// if (x.more()) { |
| 2300 | +// parse(x, jo, null, XMLParserConfiguration.ORIGINAL, 0); |
| 2301 | +// } |
| 2302 | +// } |
| 2303 | +// after.accept(jo); |
| 2304 | +// } catch (Exception e) { |
| 2305 | +// error.accept(e); |
| 2306 | +// } |
| 2307 | +// return jo; |
| 2308 | +// } |
| 2309 | +// } |
| 2310 | +private static final ExecutorService executor = Executors.newFixedThreadPool( |
| 2311 | + Runtime.getRuntime().availableProcessors() |
| 2312 | +); |
| 2313 | + |
| 2314 | + public static Future<JSONObject> toJSONObject( |
| 2315 | + Reader reader, |
| 2316 | + Consumer<JSONObject> after, |
| 2317 | + Consumer<Exception> error |
| 2318 | + ) { |
| 2319 | + FutureTask<JSONObject> task = new FutureTask<>(new FutureTaskCallable(reader, after, error)); |
| 2320 | + executor.execute(task); |
| 2321 | + return task; |
| 2322 | + } |
| 2323 | + |
| 2324 | + public static void shutdownExecutor() { |
| 2325 | + executor.shutdown(); |
| 2326 | + } |
| 2327 | + |
| 2328 | + private static class FutureTaskCallable implements Callable<JSONObject> { |
| 2329 | + private final Reader reader; |
| 2330 | + private final Consumer<JSONObject> after; |
| 2331 | + private final Consumer<Exception> error; |
| 2332 | + |
| 2333 | + public FutureTaskCallable( |
| 2334 | + Reader reader, |
| 2335 | + Consumer<JSONObject> after, |
| 2336 | + Consumer<Exception> error |
| 2337 | + ) { |
| 2338 | + this.reader = reader; |
| 2339 | + this.after = after; |
| 2340 | + this.error = error; |
| 2341 | + } |
| 2342 | + |
| 2343 | + @Override |
| 2344 | + public JSONObject call() throws Exception { |
| 2345 | + JSONObject jo = new JSONObject(); |
| 2346 | + try { |
| 2347 | + XMLTokener x = new XMLTokener(reader); |
| 2348 | + while (x.more()) { |
| 2349 | + x.skipPast("<"); |
| 2350 | + if (x.more()) { |
| 2351 | + parse(x, jo, null, XMLParserConfiguration.ORIGINAL, 0); |
| 2352 | + } |
| 2353 | + } |
| 2354 | + after.accept(jo); |
| 2355 | + return jo; |
| 2356 | + } catch (Exception e) { |
| 2357 | + error.accept(e); |
| 2358 | + throw e; |
| 2359 | + } |
| 2360 | + } |
| 2361 | + } |
| 2362 | + |
| 2363 | + public static class AsyncRunner { |
| 2364 | + private List<Future<JSONObject>> tasks = new ArrayList<>(); |
| 2365 | + |
| 2366 | + public AsyncRunner() { |
| 2367 | + } |
| 2368 | + |
| 2369 | + public void add(Future<JSONObject> task) { |
| 2370 | + this.tasks.add(task); |
| 2371 | + } |
| 2372 | + |
| 2373 | + public void run() { |
| 2374 | + boolean running = true; |
| 2375 | + while (running) { |
| 2376 | + List<Future<JSONObject>> nextTasks = new ArrayList<>(); |
| 2377 | + for (Future<JSONObject> task : tasks) { |
| 2378 | + if (!task.isDone()) { |
| 2379 | + nextTasks.add(task); |
| 2380 | + } |
| 2381 | + } |
| 2382 | + if (nextTasks.isEmpty()) { |
| 2383 | + running = false; |
| 2384 | + } else { |
| 2385 | + tasks = nextTasks; |
| 2386 | + try { |
| 2387 | + Thread.sleep(50); |
| 2388 | + } catch (InterruptedException ie) { |
| 2389 | + Thread.currentThread().interrupt(); |
| 2390 | + running = false; |
| 2391 | + } |
| 2392 | + } |
| 2393 | + } |
| 2394 | + } |
| 2395 | + } |
2240 | 2396 | } |
0 commit comments