以下处理方式有什么区别InterruptedException?最好的方法是什么?
try{
//...
} catch(InterruptedException e) {
Thread.currentThread().interrupt();
}
Run Code Online (Sandbox Code Playgroud)
要么
try{
//...
} catch(InterruptedException e) {
throw new RuntimeException(e);
}
Run Code Online (Sandbox Code Playgroud)
编辑:我也想知道这两个使用的场景.
java multithreading exception-handling interrupted-exception
Java的Thread.sleep什么时候抛出InterruptedException?忽视它是否安全?我没有做任何多线程.我只是想等几秒钟再重试一些操作.
java multithreading sleep interrupted-exception interruption
如果我创建自己的线程(即不是线程池)和我调用的某个地方sleep或任何其他可中断的方法,如果我知道代码中没有其他人在线程上执行中断,则可以忽略InterruptedException .
换句话说,如果线程应该和JVM一样长,这意味着线程不可中断,是否可以安全地假设InterruptedException 永远不会被调用,因此可以吞下异常?
java multithreading exception-handling thread-safety interrupted-exception
我最近将我的计算机更新为更强大的计算机,配备了四核超线程处理器(i7),因此可以提供大量真正的并发性.现在我在退出()我正在开发的应用程序(使用Swing GUI)时偶尔会出现以下错误System.exit(0):
Exception while removing reference: java.lang.InterruptedException
java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:118)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:134)
at sun.java2d.Disposer.run(Disposer.java:125)
at java.lang.Thread.run(Thread.java:619)
Run Code Online (Sandbox Code Playgroud)
好吧,鉴于它开始发生在具有更多并发能力的硬件上,并且它与线程有关,并且它偶尔发生,它显然是某种时间性的东西.但问题是堆栈跟踪太短了.我只有上面的列表.它根本不包括我自己的代码,因此有点难以猜测bug的位置.
以前有没有经历过这样的事情?任何想法如何开始解决它?
编辑:因为退出Swing应用程序System.exit(0)可能是"不干净",但我不想设置主框架,EXIT_ON_CLOSE因为我想确保应用程序退出时没有任何关键的事情,我添加了一个机制,以便它执行dispose()调用之前的主框架方法System.exit(0).所以它现在应该很干净,但偶尔也会发生异常.它发生在System.exit(0)被召唤之后; dispose()工作没有问题.也就是说,它必须来自一个关闭钩子:
mainFrame.dispose(); // No problem! After this returns, all visible GUI is gone.
// In fact, if there were no other threads around, the VM could terminate here.
System.exit(0); // Throws an InterruptedException from sun.java2d.Disposer.run
Run Code Online (Sandbox Code Playgroud)
我甚至尝试Window通过循环遍历Window.getWindows()数组显式处理所有s (它包含无主Dialog …
java concurrency multithreading java-2d interrupted-exception
我的代码片段:
ExecutorService executor = Executors.newSingleThreadExecutor();
try {
Task t = new Task(response,inputToPass,pTypes,unit.getInstance(),methodName,unit.getUnitKey());
Future<SCCallOutResponse> fut = executor.submit(t);
response = fut.get(unit.getTimeOut(),TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
// if the task is still running, a TimeOutException will occur while fut.get()
cat.error("Unit " + unit.getUnitKey() + " Timed Out");
response.setVote(SCCallOutConsts.TIMEOUT);
} catch (InterruptedException e) {
cat.error(e);
} catch (ExecutionException e) {
cat.error(e);
} finally {
executor.shutdown();
}
Run Code Online (Sandbox Code Playgroud)
我应该如何处理InterruptedException和ExecutionException代码?
在什么情况下,抛出这些异常?
java multithreading future executorservice interrupted-exception
Thread currentThread=Thread.currentThread();
public void run()
{
while(!shutdown)
{
try
{
System.out.println(currentThread.isAlive());
Thread.interrupted();
System.out.println(currentThread.isAlive());
if(currentThread.isAlive()==false)
{
shutdown=true;
}
}
catch(Exception e)
{
currentThread.interrupt();
}
}
}
});
thread.start();
Run Code Online (Sandbox Code Playgroud) 在Android Studio中进行调试时,在某些情况下,应用程序会崩溃并出现以下异常:
08-27 18:01:25.144 19241-24656/? E/AndroidRuntime? FATAL EXCEPTION: pool-7-thread-1
Process: com.callsign.android.dev, PID: 19241
java.lang.InterruptedException
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.reportInterruptAfterWait(AbstractQueuedSynchronizer.java:1991)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2025)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:410)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1035)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1097)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:820)
Run Code Online (Sandbox Code Playgroud)
有时候是这样的
例如,在Oneplus One或Xperia L上不会发生这种情况.
我没有找到任何其他信息来帮助我们确定问题.遇到这个问题的其他人?
在我的方法之一中,中断异常和执行异常即将到来。我像这样输入了 try catch 。
try{
//my code
}catch(InterruptedException|ExecutionException e)
Log.error(" logging it");
throw new MonitoringException("it failed" , e)
//monitoringexception extends RunTimeException
Run Code Online (Sandbox Code Playgroud)
同样在我的方法中,我抛出了 InterruptedException,ExecutionException
我在声纳中遇到严重错误 - 重新中断此方法或重新抛出“ InterruptedException”
有人知道怎么修这个东西吗。
请立即帮助。
关于Java的InterruptedException有一些有趣的问题和答案,例如Java中的InterruptedException和处理InterruptedException 的原因.但是,它们都没有告诉我InterruptedException的可能来源.
那些OS信号如SIGTERM,SIGQUIT,SIGINT?在命令行上按CTRL-C会产生InterruptedException吗?还有什么?
我在第42和43行有一个错误:Thread t1=new Thread(()->prod.test());,Thread t2=new Thread(()->cons.test()); 未处理的异常类型InterruptedException.如果我尝试quickfix它将创建带有捕获异常的try catch ,它将具有相同的错误,并将尝试以相同的方式修复它,继续用try catch包围它.
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
interface Predicate {
public void test() throws InterruptedException;
}
class MyClass {
int num = 0;
Lock lock = new ReentrantLock();
public void produce() throws InterruptedException {
lock.lock();
for (int i = 0; i < 1000; i++) {
num++;
Thread.sleep(1);
}
lock.unlock();
}
public void consume() throws InterruptedException {
lock.lock();
for (int i = 0; i < 1000; i++) {
num--; …Run Code Online (Sandbox Code Playgroud)