| 350 | } |
| 351 | |
| 352 | public boolean serviceAndCull() throws InterruptedException { |
| 353 | threadingModel.set(this); |
| 354 | |
| 355 | if (live.size() > 0) |
| 356 | System.err.println(" -- serviceAndCull, status of " + live.size() + " fibers"); |
| 357 | |
| 358 | |
| 359 | Iterator<Fiber> i = live.iterator(); |
| 360 | Set<Fiber> repost = new LinkedHashSet<>(); |
| 361 | while (i.hasNext()) { |
| 362 | Fiber f = i.next(); |
| 363 | |
| 364 | System.err.println(" " + f + " runner done ? " + f.runner.isDone() + " / " + f.runner.isCancelled() + " paused ? " + f.paused); |
| 365 | if (f.runner.isDone()) { |
| 366 | f.wasPaused = false; |
| 367 | if (f.exception != null) { |
| 368 | i.remove(); |
| 369 | if (f.handler != null) |
| 370 | f.handler.accept(f.exception); |
| 371 | else |
| 372 | throw new IllegalStateException(f.exception); |
| 373 | } else { |
| 374 | |
| 375 | System.err.println(" done, polling one more time"); |
| 376 | Object o = f.output.poll(); |
| 377 | System.out.println(" got :" + o); |
| 378 | if (o != null) { |
| 379 | f.out.accept(o); |
| 380 | f.lastReturn = o; |
| 381 | } |
| 382 | System.out.println(" that's it for this fiber"); |
| 383 | i.remove(); |
| 384 | } |
| 385 | } else { |
| 386 | Object p = f.paused.get(); |
| 387 | f.wasPaused = true; |
| 388 | |
| 389 | if (p == null) continue; |
| 390 | |
| 391 | if (p instanceof Collection) { |
| 392 | if (((Collection) p).size() == 0) { |
| 393 | repost.add(f); |
| 394 | continue; |
| 395 | } |
| 396 | Iterator q = ((Collection) p).iterator(); |
| 397 | q.next(); |
| 398 | q.remove(); |
| 399 | } |
| 400 | |
| 401 | if (p instanceof Boolean) { |
| 402 | if (((Boolean) p).booleanValue()) { |
| 403 | repost.add(f); |
| 404 | continue; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | f.wasPaused = false; |
| 409 | |