Java的Thread.sleep什么时候抛出InterruptedException?忽视它是否安全?我没有做任何多线程.我只是想等几秒钟再重试一些操作.
java multithreading sleep interrupted-exception interruption
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) 如果我有一个带有try/finally部分的函数,并且运行它的线程在try块中被中断,那么finally块是否会在中断实际发生之前执行?
我有一个应用程序,它在一个单独的部分执行一些后台任务(网络侦听和阅读)Thread.但是,当我关闭应用程序时,似乎Thread没有被终止/中止(单击标题栏上的"x"按钮).是因为主线程例程是while(true) {...}?这里有什么解决方案?我正在为Thread寻找一些"中断"标志作为"while"循环的条件,但没有找到任何.
所以在我的应用程序中,在iOS 6上运行,一切似乎都可以正常使用音频.我使用旧的C API格式来捕获使用回调的中断; 通过设置:AudioSessionInitialize(NULL, NULL, interruptionListenerCallback, (__bridge void *)self)它很棒.但是,使用iOS 7 SDK时,似乎在设备接到呼叫或闹钟响起时,我的中断回调永远不会被调用.
经过一番环顾,我听说旧的C api已被弃用,你应该转向更新的AVAudioSession功能.更多阅读显示AVAudioSession委托已被弃用,您应该使用NSNotificationfor AVAudioSessionInterruptionNotification来捕获中断并做任何需要完成的事情.
对我来说,似乎这个通知从未被实际触发,因此我从未得到适当的中断,然后在通话结束后打破了我的所有音频内容.
我这样注册通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(AudioInterruption:) name:AVAudioSessionInterruptionNotification object:nil];
Run Code Online (Sandbox Code Playgroud)
现在,该AudioInterruption:函数只是记录它被触发了.日志和任何断点都没有触发.
要清楚,最初音频播放和录制工作正常.发生中断时(例如来电或闹钟),不会触发中断通知.如果需要更多周围代码,请告诉我.
我正在为Haskell中的Scheme解释器实现一个REPL,我想处理一些像UserInterrupt,StackOverflow,HeapOverflow等异步事件......基本上,我想在UserInterrupt发生时停止当前的计算并打印一个StackOverflow和HeapOverflow发生时的合适消息等.我实现如下:
repl evaluator = forever $ (do
putStr ">>> " >> hFlush stdout
out <- getLine >>= evaluator
if null out
then return ()
else putStrLn out)
`catch`
onUserInterrupt
onUserInterrupt UserInterrupt = putStrLn "\nUserInterruption"
onUserInterrupt e = throw e
main = do
interpreter <- getMyLispInterpreter
handle onAbort (repl $ interpreter "stdin")
putStrLn "Exiting..."
onAbort e = do
let x = show (e :: SomeException)
putStrLn $ "\nAborted: " ++ x
Run Code Online (Sandbox Code Playgroud)
它按预期工作,但有一个例外.如果我启动解释器并按Ctrl-Z + Enter,我会得到:
>>> ^Z
Aborted: <stdin>: hGetLine: end of …Run Code Online (Sandbox Code Playgroud) haskell exception-handling exception interruption read-eval-print-loop
synchronized (Foo.class) {
while (someCondition) {
try {
Foo.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
似乎这个线程在其他一些线程调用时interrupt()或notify()在此线程上都会唤醒.这两者有什么不同吗?
- 编辑 -
我知道一个用于通知一个对象,另一个用于中断一个线程.但是这两者都导致了相同的结果,也就是说,这个线程被唤醒了,所以我想问的是这两种情况的后果是如何相互不同的.
我试图弄清楚实际发生了几周的事情,我不知道为什么我不能在中断后继续播放,所以你们可能知道答案.AudioSessionSetActive(TRUE)总是返回'!cat',这是kAudioSessionIncompatibleCategory,重新激活,如果我的应用程序在后台播放,我在不同的应用程序.虽然它工作正常并且如果我在我的应用程序中发现中断时继续播放.
原始代码实际上包含在宏中包含的所有AudioSession和AudioQueue调用,如果它意味着错误,则打印OSStatus,但我删除它以获得更好的可读性.此外,[self pause]只是暂停,所以基本上它会在upause时调用AudioQueueStart(audioQueue,NULL)但是如果AudioSession失败则它不起作用.
音频会话初始化代码:
AudioSessionInitialize(NULL, NULL, _audioSessionInterruptionListener, self);
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, _audioSessionPropertyListener, self);
AudioSessionSetActive(TRUE);
Run Code Online (Sandbox Code Playgroud)
中断处理程序代码:
- (void)handleInterruptionChangeToState:(AudioQueuePropertyID)inInterruptionState
{
if(inInterruptionState == kAudioSessionBeginInterruption)
{
NSLog(@"+Interruption");
if(self.state == NX_STATE_PLAY)
{
[self pause];
AudioSessionSetActive(FALSE);
isPausedByInterruption = YES;
}
}
else if(inInterruptionState == kAudioSessionEndInterruption)
{
if(isPausedByInterruption)
{
AudioSessionSetActive(TRUE);
[self pause];
isPausedByInterruption = FALSE;
}
NSLog(@"-Interruption");
}
}
Run Code Online (Sandbox Code Playgroud)
这个流媒体源代码可以在这里找到https://bitbucket.org/and/amaudiostreamer/src/122de41fe6c0/AMAudioStreamer/AMAudioStreamer/Classes/NxAudioStreamer.m,如果它能帮助以某种方式解决问题..
我一直在玩(SpriteKit)游戏时打电话来测试中断.我正在使用ObjectAL文档中的示例:"使用OpenAL对象和OALAudioTrack".
所以,我让图书馆自动处理这个......
[OALAudioSession sharedInstance ]. handleInterruptions = YES
Run Code Online (Sandbox Code Playgroud)
它有效但部分有效.例如,通过3声音的简单设置,我得到下一条错误消息:
OALAudioSession activateAudioSession]:2次尝试后无法激活音频会话:Error Domain = NSOSStatusErrorDomain Code = 561015905"操作无法完成.(OSStatus错误561015905.)"
Error 561015905 == 0x21706C61 == !pla,并引用AVAudioSession.h中声明的错误:
AVAudioSessionErrorCodeCannotStartPlaying ='!pla',/*0x21706C61,561015905
实际上这是有效的,有两次失败的尝试,第三次是成功的,没有什么可以被注意到,因为一切都很快,一切似乎都应该正常工作.
我注意到如果我添加更多声音(比方说20),我会收到相同的消息:
20次尝试后无法激活音频会话:
之后,会话被激活.然后,我刚刚在相关方法中添加了调试消息:
OALAudioSession.m
- (void) activateAudioSession
{
NSError* error;
for(int try = 1; try <= kMaxSessionActivationRetries; try++)
{
if([[AVAudioSession sharedInstance] setActive:YES error:&error])
{
NSLog(@"Session activated after %d", try);
audioSessionActive = YES;
return;
}
OAL_LOG_ERROR(@"Could not activate audio session after %d tries: %@", try, error);
[NSThread sleepForTimeInterval:0.2];
}
OAL_LOG_ERROR(@"Failed to activate …Run Code Online (Sandbox Code Playgroud) 我试图在ubuntu中设置串口的中断(在用C编写的程序中),但它不起作用.我已经检查过串口通讯是否正常运行而没有中断,所以我可能会设置错误.代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <errno.h>
#include <termios.h>
void signal_handler_IO (int status); /* definition of signal handler */
int n;
int fd;
int connected;
struct termios termAttr;
struct sigaction saio;
int main(int argc, char *argv[])
{
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
perror("open_port: Unable to open /dev/ttyO1\n");
exit(1);
}
saio.sa_handler = signal_handler_IO;
saio.sa_flags = 0;
saio.sa_restorer = NULL; …Run Code Online (Sandbox Code Playgroud) interruption ×10
java ×4
interrupt ×2
objective-c ×2
abort ×1
audio ×1
audioqueue ×1
c ×1
c# ×1
exception ×1
haskell ×1
ios ×1
iphone ×1
notify ×1
objectal ×1
serial-port ×1
sleep ×1
sprite-kit ×1
terminate ×1
try-catch ×1
ubuntu ×1
wait ×1