如何从一个线程抛出一个未经检查的异常传播到主线程?

时间:2017-11-09 15:27:22

标签: java multithreading stream completable-future

我找不到一种方法(既不通过SO也不调试我的代码)允许从胎面抛出的异常传播到主线程。我已尝试使用Thread.setUncaughtExceptionHandler()并使用CompletableFuture.exceptionally().handle(),...

关键在于使用这些机制(我调试),实际处理是在工作线程上执行的,而不是主线程 - 我无法设法将它传递给主线程。

总的来说,我正在编写一个测试,如果异常在工作线程中出现,它就永远不会进入运行测试的主线程,即使出现问题也会使测试通过。 / p>

我需要异常会引发异步;我不能等待将来完成,因为我需要立即从主踏板返回一个Stream(一个PipedStream)而不会阻塞。

我得到的唯一提示是控制台错误(仅当我使用传统的Thread + uncaughtExceptionHandler方法时,当我尝试使用CompletableFuture时,根本没有日志记录):

Exception: com.example.exception.MyException thrown from the UncaughtExceptionHandler in thread "Thread-5"

或者如果我没有定义异常处理程序:

Exception in thread "Thread-5" com.example.MyException: Exception message

我提供了一些代码:

try {
    @SuppressWarnings("squid:S2095") final PipedOutputStream pos = new PipedOutputStream();
    final PipedInputStream pis = new PipedInputStream(pos);

    CompletableFuture.runAsync(pipeDecryptorRunnable(inputStream, pos));

    return pis;

} catch (IOException e) {
    throw new CryptographyException(e.getMessage(), e);
}

Inside pipeDecryptorRunnable是一个解密数据的CipherStream。抛出异常。但我无法在主线程中捕获它并变得不可见。从此方法返回的pis流用于读取,工作线程在读取prom pis时即时解密数据。

编辑: UncaughtExceptionHandler因为类似问题中的答案表明不适用于我的场景,因为处理程序代码是由工作线程调用的,而不是主线程调用。

1 个答案:

答案 0 :(得分:0)

感谢@RalfKleberhoff的提示,我找到了我想要的解决方案。事实是,为了实现期望的行为,需要线程间通信机制。鉴于我已经在使用PipedStream,我可以利用它来实现目标 - 我还想到了某种涉及事件总线的解决方案,从一个线程向另一个线程发出信号或通信(主线程/工作线程)我认为一些事件总线库也可以实现它。

回到管道流,我在主线程中读取pis,并在工作线程中写入连接pos。因此,当工作者引发异常时,我需要主线程注意到这一点。

要实现这一点,您可以扩展PipedOutputStream类,添加一个方法,以便在发生异常时向连接的管道发出信号。同样,您需要扩展连接的PipedInputStream以便在异常时发出信号,存储异常并覆盖读取方法以检查是否首先发生异常,并且在这种情况下,抛出包含在异常中的异常读取方法的IOException

这里是代码:

/**
 * This piped stream class allows to signal Exception between threads, allowing an exception produced in the writing
 * thread to reach the reading thread.
 *
 * @author Gerard on 10/11/2017.
 */
public class ExceptionAwarePipedOutputStream extends PipedOutputStream {

    private final ExceptionAwarePipedInputStream sink;

    public ExceptionAwarePipedOutputStream(ExceptionAwarePipedInputStream sink) throws IOException {
        super(sink);
        this.sink = sink;
    }

    /**
     * Signals connected {@link ExceptionAwarePipedInputStream} that an exception ocurred allowing to propagate it
     * across respective threads. This works as inter thread communication mechanism. So it allows to the reading thread
     * notice that an exception was thrown in the writing thread.
     *
     * @param exception The exception to propagate.
     */
    public void signalException(Throwable exception) {
        sink.signalException(exception);
    }
}

·

/**
 * This piped stream class allows to signal Exception between threads, allowing an exception produced in the writing
 * thread to reach the reading thread.
 *
 * @author Gerard on 10/11/2017.
 */
public class ExceptionAwarePipedInputStream extends PipedInputStream {

    private volatile Throwable exception;

    void signalException(Throwable exception) {
        this.exception = exception;
    }

    @Override
    public int read(byte[] b) throws IOException {
        final int read = super.read(b);
        checkException();
        return read;
    }

    @Override
    public synchronized int read() throws IOException {
        final int read = super.read();
        checkException();
        return read;
    }

    @Override
    public synchronized int read(byte[] b, int off, int len) throws IOException {
        final int read = super.read(b, off, len);
        checkException();
        return read;
    }

    private void checkException() throws IOException {
        if (exception != null) {
            throw new IOException(exception.getMessage(), exception);
        }
    }
}

客户端代码:

public InputStream decrypt(InputStream inputStream) {

    assert supportedStreamModes.contains(mode) : "Unsupported cipher mode for stream decryption " + mode;

    @SuppressWarnings("squid:S2095") final ExceptionAwarePipedInputStream pis = new ExceptionAwarePipedInputStream();
    final ExceptionAwarePipedOutputStream pos = newConnectedPipedOutputStream(pis);
    final Cipher decryptor = newDecryptorInitialized(inputStream);

    CompletableFuture.runAsync(
        pipeDecryptorRunnable(inputStream, pos, decryptor));

    return pis;
}

private ExceptionAwarePipedOutputStream newConnectedPipedOutputStream(ExceptionAwarePipedInputStream pis) {
    try {
        return new ExceptionAwarePipedOutputStream(pis);
    } catch (IOException e) {
        throw new CryptographyException(e.getMessage(), e);
    }
}

异常处理部分(通知线程信令):

private Runnable pipeDecryptorRunnable(InputStream inputStream, ExceptionAwarePipedOutputStream pos, Cipher decryptor) {
    return () -> {
        try {

            // do stuff... and write to pos

        } catch (Exception e) {
            // Signaling any (checked or unchecked) exception
            pos.signalException(new CryptographyException(e.getMessage(), e));
        } finally {
            closePipedStream(pos);
        }
    };
}