CompletableFuture.supplyAsync(
() -> {
transporter.write(req);
//here take the value from a blocking queue,will throw a interruptedException
return responseQueue.take();
}, executorService);
Il metodo comune per affrontare InterruptedException è o interrompere di nuovo o diretta tiro InterruptedException, ma entrambi non possono lavorare. Qualcuno ha l'idea?JDK8 CompletableFuture.supplyAsync come affrontare InterruptedException
"* ma entrambi non possono funzionare. *" => Perché? – assylias
hanno entrambi errori di compilazione. se l'eccezione di lancio diretto, il compilatore mostrerà un'eccezione non gestita, se la cattura e chiama Thead.current.interrupt, il compilatore mostrerà deve restituire un tipo T. – GrapeBaBa
Sì, è necessario restituire o lanciare. Se si decide di restituire null, ad esempio: 'try {return queue.take(); } catch (InterruptedException e) {Thread.currentThread(). interrupt(); return null; } ' – assylias