Created
July 14, 2014 21:04
-
-
Save jnorthrup/b1e887b4ccb684523b5a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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