ios 异步线程 NSLock 小结
来源:互联网 发布:织梦cms仿站教程 编辑:程序博客网 时间:2024/05/19 01:12
oc
1
dispatch_queue_t q =dispatch_queue_create("thread-one",DISPATCH_QUEUE_SERIAL) ; dispatch_async(q, ^{ //to do }); // 获取 名字的方法 // NSLog(@"%s", dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL));
2
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(display) object:nil]; thread.name = @"thread-three"; [thread start];
swfit
1
let queue1 = DispatchQueue(label: "thread-one") queue1.async { // to do ; }
2
let thread1 = Thread(target: self, selector: #selector(ViewController.display), object: nil) thread1.name = "thread1" thread1.start()
Nslock使用
let thread1 = Thread(target: self, selector: #selector(ViewController.display), object: nil) thread1.name = "thread1" thread1.start() let thread2 = Thread(target: self, selector: #selector(ViewController.display), object: nil) thread2.name = "thread2" thread2.start() let thread3 = Thread(target: self, selector: #selector(ViewController.display), object: nil) thread3.name = "thread3" thread3.start()
func display(){ // lock.lock() //打开 //耗时操作 self.name = Thread.current.name! ; // Thread.sleep(forTimeInterval: TimeInterval(5)) print("\(self.name) -- \(Thread.current.name!) ") // lock.unlock()//打开 }
输出
thread2 -- thread3 thread2 -- thread1 thread2 -- thread2
混乱了,修改display方法 注释打开 输出
thread3 -- thread3 thread1 -- thread1 thread2 -- thread2
正常了
func display(){ lock.lock() self.name = Thread.current.name! let timer:Timer = Timer(timeInterval: 5.0, target: self, selector: #selector(ViewController.log), userInfo: nil, repeats: false) // 将定时器添加到运行循环 RunLoop.current.add(timer, forMode: RunLoopMode.defaultRunLoopMode) RunLoop.current.run() lock.unlock() } func log(){ print("\(self.name) -- \(Thread.current.name!) ") Thread.sleep(forTimeInterval: 10); }
如果 lock 的内容 有异步执行的 会等异步执行完 再解锁 上面 一共过了 15s 才解锁
0 0
- ios 异步线程 NSLock 小结
- ios 异步线程 NSLock 小结
- iOS线程锁NSLock案例(卖火车票)
- iOS多线程编程:线程同步总结(NSLock)
- iOS经典讲解之线程锁NSLock案例(卖火车票)
- [ios]NSLock锁
- [ios]NSLock锁
- IOS线程数据篇4之线程之间的同步:synchronized NSlock pthread_mutex_t dispatch_semaphore_create
- ios中常用的线程同步策略-synchronized和NSLock
- iOS锁synchronized OSSpinlock NSLock
- iOS小结之多线程
- iOS小结之多线程
- 线程锁 ---- NSLock , @synchronized 和 dispatch_semaphore_t
- IOS开发之NSLock 的使用
- IOS NSLocking协议,NSLock,NSConditionLock,NSRecursiveLock,NSCondition
- 进程,线程,同步,异步,内存管理小结
- IOS-线程异步操作GCD
- IOS-线程异步操作dispatch_queue_t
- Java登录网站多个账号整理斗米简历(二)
- [LeetCode OJ]Split Array Largest Sum
- SQLite介绍
- 为什么嵌入式系统需要具有实时操作系统的特性?
- 手机快充芯片及其技术标准和设计原理详解
- ios 异步线程 NSLock 小结
- 分布式架构学习之:024--Dubbo直连服务提供者
- 天气接口页面查询demo
- 如何寻找最新版本的Oracle Database独立安装包
- 关于json、jsonp
- oracle 服务器发送邮件
- Fragment 全解析(1):那些年踩过的坑
- ZUFE2480: 神奇的序列
- jfreechart 的使用