美文网首页
GCD处理任务和runloop的关系

GCD处理任务和runloop的关系

作者: 月禅 | 来源:发表于2023-03-29 17:23 被阅读0次
var count = 0
    var timer: Timer? = nil
    func test() {
        let queue = DispatchQueue(label: "aaa")
        queue.async {
            print("start\(Thread.current)")
            queue.asyncAfter(deadline: DispatchTime(uptimeNanoseconds: 1), execute: DispatchWorkItem(block: {
                print("1s后执行异步任务\(Thread.current)")
            }))
            self.perform(#selector(self.perfromTask), with: nil, afterDelay: 2)
            self.timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in
                print("定时任务\(Thread.current)")
                self.count += 1
                if self.count > 10 {
                    self.timer?.invalidate()
                    self.timer = nil
                }
            }
            RunLoop.current.add(self.timer!, forMode: .common)
            RunLoop.current.run()
        }
    }
    
    
    @objc func perfromTask() {
        print("perform 延时任务\(Thread.current)")
    }
    /**
     start<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}
     定时任务<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}
     perform 延时任务<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}
     定时任务<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}
     定时任务<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}
     定时任务<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}
     定时任务<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}
     定时任务<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}
     定时任务<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}
     定时任务<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}
     定时任务<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}
     定时任务<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}
     定时任务<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}
     1s后执行异步任务<NSThread: 0x6000000bc6c0>{number = 7, name = (null)}     //(timer任务会卡住当前线程)
     */

相关文章

  • NSTimer不准时问题解决

    NSTimer依赖于RunLoop,如果RunLoop的任务过于繁重,可能会导致NSTimer不准时; GCD的定...

  • GCD定时器的封装

    NSTimer依赖于Runloop,如果Runloop的任务过于繁重,可能会导致NSTimer不准时。而GCD定时...

  • iOS实现一个更精准的定时器

    NSTimer依赖于RunLoop,如果RunLoop的任务过于繁重,可能会导致NSTimer不准时 使用GCD实...

  • GCD定时器

    NSTimer依赖于RunLoop,如果RunLoop的任务过于繁重,可能会导致NSTimer不准时 而GCD的定...

  • GCD定时器

    NSTimer依赖于RunLoop,如果RunLoop的任务过于繁重,可能会导致NSTimer不准时 而GCD的定...

  • 多线程:GCD定时器

    GCD定时器 NSTimer依赖于RunLoop,如果RunLoop的任务过于繁重,可能会导致NSTimer不准时...

  • iOS NSTimer没有GCD Timer准时的原因

    NSTimer不准时的原因:1:RunLoop循环处理的时间2:受RunLoop模式的影响 gcd的timer与N...

  • ios面试题

    runtime介绍 runLoop与多线程关系 多线程原理(GCD、NSOperation) AFNetworki...

  • Objective - C 内存管理(二)GCD定时器

    GCD定时器的使用场景 NSTimer依赖于RunLoop,如果RunLoop的任务过于繁重,可能会导致NSTim...

  • dispatch_source_t 定时器

    NSTimer受runloop的影响, 由于runloop需要处理很多任务(scrollView 滑动等), 导致...

网友评论

      本文标题:GCD处理任务和runloop的关系

      本文链接:https://www.haomeiwen.com/subject/nlwfddtx.html