ios NSThread sleepfortimeinterval 阻塞主线程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18249749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:45:00  来源:igfitidea点击:

NSThread sleepfortimeinterval blocks main thread

iosnsthread

提问by user1028028

I want to simulate a communication with a server. As the remote server will have some delays I want to use a background thread that has on it

我想模拟与服务器的通信。由于远程服务器会有一些延迟,我想使用一个后台线程

 [NSThread sleepForTimeInterval:timeoutTillAnswer];

The thread is created with NSThread sub classing and started ... However I noticed that sleepForTimeInterval is blocking the main thread... Why??? Isn't a NSThread a backgroundThread by default?

该线程是用 NSThread 子类创建并启动的......但是我注意到 sleepForTimeInterval 正在阻塞主线程......为什么???默认情况下 NSThread 不是 backgroundThread 吗?

This is how the thread is created:

这是线程的创建方式:

   self.botThread = [[PSBotThread alloc] init];
    [self.botThread start];

Further info: This is the bot thread subclas

更多信息:这是机器人线程子类

- (void)main
{
    @autoreleasepool {
        self.gManager = [[PSGameManager alloc] init];
        self.comManager = [[PSComManager alloc] init];
        self.bot = [[PSBotPlayer alloc] initWithName:@"Botus" andXP:[NSNumber numberWithInteger:1500]];
        self.gManager.localPlayer = self.bot;
        self.gManager.comDelegate = self.comManager;
        self.gManager.tillTheEndGame = NO;
        self.gManager.localDelegate = self.bot;
        self.comManager.gameManDelegate = self.gManager;
        self.comManager.isBackgroundThread = YES;
        self.comManager.logginEnabled = NO;
        self.gManager.logginEnabled = NO;
        self.bot.gameDelegate = self.gManager;
        BOOL isAlive = YES;
        // set up a run loop
        NSRunLoop *runloop = [NSRunLoop currentRunLoop];
        [runloop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
        [self.gManager beginGameSP];
        while (isAlive) { // 'isAlive' is a variable that is used to control the thread existence...
            [runloop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
        }



    }
}

- (void)messageForBot:(NSData *)msg
{
    [self.comManager didReceiveMessage:msg];
}

I want to call "messageForBot" from the main thread... also the background thread should call a method on the main thread to communicate.. The sleep for time intervail in inside the gManager object ....

我想从主线程调用“messageForBot”……后台线程也应该调用主线程上的一个方法来进行通信……gManager 对象内部的睡眠时间间隔……

回答by John

It blocks whatever thread sleepForTimeInterval is running on. Run it on another thread to simulate your server delay like this:

它会阻止 sleepForTimeInterval 正在运行的任何线程。在另一个线程上运行它来模拟您的服务器延迟,如下所示:

dispatch_queue_t serverDelaySimulationThread = dispatch_queue_create("com.xxx.serverDelay", nil);
dispatch_async(serverDelaySimulationThread, ^{
     [NSThread sleepForTimeInterval:10.0];
     dispatch_async(dispatch_get_main_queue(), ^{
            //Your server communication code here
    }); 
});

回答by sbarow

Try create a method in your thread class called sleepThread

尝试在您的线程类中创建一个名为 sleepThread 的方法

-(void)sleepThread
{
   [NSThread sleepForTimeInterval:timeoutTillAnswer];
}

Then to make it sleep from your main thread

然后让它从你的主线程休眠

[self.botThread performSelector:@selector(sleepThread) onThread:self.botThread withObject:nil waitUntilDone:NO];

To send updated to your main thread from your bot thread.

从您的机器人线程发送更新到您的主线程。

dispatch_async(dispatch_get_main_queue(), ^{
    [MainClass somethinghasUpdated];
});

Side Note

边注

To create the RunLoop I think all you need to do is

要创建 RunLoop 我认为你需要做的就是

// Run the Current RunLoop
[[NSRunLoop currentRunLoop] run];

回答by Brian

Swift:

迅速:

let nonBlockingQueue: dispatch_queue_t = dispatch_queue_create("nonBlockingQueue", DISPATCH_QUEUE_CONCURRENT)
dispatch_async(nonBlockingQueue) {
    NSThread.sleepForTimeInterval(1.0)
    dispatch_async(dispatch_get_main_queue(), {
        // do your stuff here
    })
}