草庐IT

InterruptedException

全部标签

go - 检测 Go 例程中断

有没有办法检测go例程在执行时是否被中断?我想要类似于Java中的InterruptedException的东西:https://docs.oracle.com/javase/8/docs/api/java/lang/InterruptedException.html 最佳答案 InterruptedException如果线程被中断,则抛出Java中的异常,例如与Thread.interrupt()方法(当它正在等待、sleep或以其他方式占用时)。在Go中,您不能从外部中断goroutine(参见cancelablockingop

java - 如何优雅地中断 Windows 中的阻塞等待?

我需要编写一个执行阻塞I/O操作的JNI接口(interface)并且我需要这些方法可以被中断。例如:interfaceIO{nativevoidwaitForEvents()throwsInterruptedException,IOException;nativeintreadBytes(byte[]data,intoffset,intlen)throwsInterruptedException,IOException;}在Win32上,我使用WindowsAPI“waitForSingleObject(HADLE)”来实现“waitForEvents”,并使用“read(HANDL

什么是创建主()等待Java线程的Main()循环的黄金标准

我的任务是编写一个小型服务器应用程序。它应该通过控制台启动,然后在后台运行,处理一些网络流量并在本地计算内容,直到收到关闭信号为止。我很确定我可以处理所有这些-除了非常基本的应用程序体系结构。我非常不确定如何带我的主循环等待应用程序完成。因此,这是我当前的代码,清理并省略了不必要的零件。publicclassTestServer{publicstaticLoggerlogger;privatestaticBooleanabortStartup=false;privatestaticServerModuleserver;publicstaticvoidmain(String[]args){Sys

java - 我如何使用 Jython 线程,因为它们是 Java 线程?

例如,我想在Jython中重现此线程,因为我需要从JavaAPI启动我的状态机。我对Jython了解不多。我怎样才能做到这一点?Threadthread=newThread(){@Overridepublicvoidrun(){statemachine.enter();while(!isInterrupted()){statemachine.getInterfaceNew64().getVarMessage();statemachine.runCycle();try{Thread.sleep(100);}catch(InterruptedExceptione){interrupt();

java - 为什么我们不应该吞下 InterruptedException

我很困惑,无法理解为什么不应该吞下InterruptedException。IBM的文章说当阻塞方法检测到中断并抛出InterruptedException时,它会清除中断状态。如果您捕捉到InterruptedException但无法重新抛出它,您应该保留中断发生的证据以便调用堆栈上层的代码可以了解中断并在需要时做出响应publicclassTaskRunnerimplementsRunnable{privateBlockingQueuequeue;publicTaskRunner(BlockingQueuequeue){this.queue=queue;}publicvoidrun

java - InterruptedException的原因

从J2me文档我们知道:java.lang.InterruptedException当一个线程正在等待、hibernate或以其他方式长时间暂停并且另一个线程中断它时抛出。问题是,如果我从一个线程为另一个线程调用Thread.Interupt(),其中另一个线程的Run()方法等待InputStream.Read(char[]buf),是否有可能得到这样的异常? 最佳答案 响应线程中断而阻塞读取的行为实际上是未定义的。参见thislong-standingbug了解详情。缺点是有时会得到EOF,有时会得到IOException。

java - 修复错误 : Unreported Exception InterruptedException

我是Java新手。我刚刚在搜索如何让Java程序等待,它说使用Thread.sleep()方法。但是,当我这样做时出现错误:error:unreportedexceptionInterruptedException;mustbecaughtordeclaredtobethrown我通过在方法声明中添加throwsInterruptedException来解决这个问题,现在它可以工作了。但是,调用方法的时候,又报错了。人们说要使用throwandcatchblock,但我还不确定该怎么做。有人可以帮我吗?无论如何,Draw.java的代码(使用sleep()方法):packagegrap

Java 并发 JDK 1.6 : Busy wait does better than signalling? Effective Java #51

JoshuaBloch的“EffectiveJava”,第51条不是关于依赖线程调度程序以及不要将线程不必要地保持在可运行状态。引用文本:ThemaintechniqueforkeepingthenumberofrunnablethreadsdownistohaveeachthreaddoasmallamountofworkandthenwaitforsomeconditionusingObject.waitorforsometimetoelapseusingThread.sleep.Threadsshouldnotbusy-wait,repeatedlycheckingadatast

java - Swing JTextArea 多线程问题 - InterruptedException

我有一个简单的控制台应用程序,它在多个线程(其中10-20个)中运行计算。现在我正在尝试创建一个简单的GUI,它允许我选择要处理的文件并从所有线程打印日志。因此,我为我的日志创建了一个带有JTextArea的swingGUI,并创建了一个将信息记录到日志的方法:publicsynchronizedvoidlog(Stringtext){logArea.append(text);logArea.append("\n");if(logArea.getDocument().getLength()>50000){try{logArea.getDocument().remove(0,5000);

java - Thread.sleep 情况下的异常处理和 Threads 情况下的 wait() 方法

我正在尝试编写一个生产者消费者模型(java中的生产者线程和消费者线程)我想知道如何处理Thread.sleep方法和Object类的wait()抛出的InterruptedException方法packageproducerconsumer;importjava.util.ArrayList;publicclassConsumerimplementsRunnable{ArrayListcontainer;@Overridepublicvoidrun(){while(true){System.out.println("ConsumerThreadRunning");try{Thread