Skip to content

Instantly share code, notes, and snippets.

@jnorthrup
Created July 14, 2014 21:04
Show Gist options
  • Save jnorthrup/b1e887b4ccb684523b5a to your computer and use it in GitHub Desktop.
Save jnorthrup/b1e887b4ccb684523b5a to your computer and use it in GitHub Desktop.
/**
* handles SslEngine state NEED_TASK.creates a phaser and launches all threads with invokeAll
*
* @param key
* @param sslEngine
* @param executorService
* @return
*/
public static int delegateTasks(final SelectionKey key, SSLEngine sslEngine, ExecutorService executorService) {
final int origOps = key.interestOps();
List<Callable<Void>> runnables = new ArrayList<>();
final Phaser phaser = new Phaser();
Runnable t;
while (null != (t = sslEngine.getDelegatedTask())) {
final Runnable finalT1 = t;
runnables.add(new Callable<Void>() {
@Override
public Void call() throws Exception {
phaser.register();
finalT1.run();
phaser.arrive();
return null;
}
});
}
runnables.add(new Callable<Void>() {
@Override
public Void call() throws Exception {
phaser.register();
phaser.awaitAdvanceInterruptibly(phaser.arrive());
key.interestOps(origOps).selector().wakeup();
return null;
}
});
try {
key.interestOps(0);
executorService.invokeAll(runnables);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment