在Java中,我使用FutureTask通过Executor异步运行Callable。为了检查调用线程是否发生错误,我需要从Callable中抛出潜在的异常。但是,这会导致打印到System.out的堆栈跟踪(我调试过,这发生在LockSupport.unpark(Thread)的Unsafe.unpark(Thread)中。)
我有什么想法可以防止这种情况发生?
我之前已经将异常记录到真正的记录器(通过SLF4J),并且从不希望在System.out上直接进行堆栈跟踪。
更新:我想我已经指出了相关信息,但澄清了所有问题: 此测试用例可以重现问题(方法testTransferStreamAsyncCallbackFail()): https://github.com/m-m-m/util/blob/master/io/src/test/java/net/sf/mmm/util/io/base/StreamUtilTest.java
使用的实现是:
public AsyncTransferrer transferAsync(InputStream inStream, OutputStream outStream, boolean keepOutStreamOpen, TransferCallback callback) { StreamTransferrer transferrer = new StreamTransferrer(inStream, outStream, keepOutStreamOpen, callback); AsyncTransferrerImpl task = new AsyncTransferrerImpl(transferrer); this.executor.execute(task); return task; }
我通过eniter代码进行了调试,但绝对不是我的代码记录到控制台。这是在我试图解释的JDK中完成的。
回答问题的其他更新:
我使用的执行程序是Executors.defaultThreadFactory() 正如你在这里看到的: https://github.com/m-m-m/util/blob/master/core/src/main/java/net/sf/mmm/util/concurrent/base/SimpleExecutor.java#L30 https://github.com/m-m-m/util/blob/master/io/src/main/java/net/sf/mmm/util/io/base/StreamUtilImpl.java#L165
输出是我从Callable中输入的Exception中的异常的堆栈跟踪:
java.util.concurrent.ExecutionException: net.sf.mmm.util.io.api.RuntimeIoException: IO: An unexpetected error has occurred while copying data! 23355443-dae9-4f4e-87a2-fe38b8a3d513 at java.util.concurrent.FutureTask.report(FutureTask.java:122) at java.util.concurrent.FutureTask.get(FutureTask.java:192) at net.sf.mmm.util.io.base.StreamUtilImpl$AsyncTransferrerImpl.get(StreamUtilImpl.java:1) at net.sf.mmm.util.io.base.StreamUtilTest.testTransferStreamAsyncCallbackFail(StreamUtilTest.java:171) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) Caused by: net.sf.mmm.util.io.api.RuntimeIoException: IO: An unexpetected error has occurred while copying data! 23355443-dae9-4f4e-87a2-fe38b8a3d513 at net.sf.mmm.util.io.base.StreamUtilImpl$BaseTransferrer.transfer(StreamUtilImpl.java:533) at net.sf.mmm.util.io.base.StreamUtilImpl$BaseTransferrer.call(StreamUtilImpl.java:583) at net.sf.mmm.util.io.base.StreamUtilImpl$BaseTransferrer.call(StreamUtilImpl.java:1) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.lang.Thread.run(Thread.java:745) Caused by: java.io.IOException: This is a test! at net.sf.mmm.util.io.base.StreamUtilTest.testTransferStreamAsyncCallbackFail(StreamUtilTest.java:155) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)