Swift - 多线程实现方式(1) - NSThread
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
import UIKitclass ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() //方式1:使用类方法 NSThread.detachNewThreadSelector("downloadImage", toTarget: self, withObject: nil) //方式2:实例方法-便利构造器 var myThread:NSThread = NSThread(target: self, selector: "downloadImage", object: nil) myThread.start() } //定义一个下载图片的方法,线程调用 func downloadImage(){ var data = NSData(contentsOfURL: NSURL(string: imageUrl)!, options: nil, error: nil) println(data?.length) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }} |
4,线程同步
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
import UIKitclass ViewController: UIViewController { //定义两个线程 var thread1:NSThread? var thread2:NSThread? //定义两个线程条件,用于锁住线程 let condition1 = NSCondition() let condition2 = NSCondition() override func viewDidLoad() { super.viewDidLoad() thread2 = NSThread(target: self, selector: "method2:", object: nil) thread1 = NSThread(target: self, selector: "method1:", object: nil) thread1?.start() } //定义两方法,用于两个线程调用 func method1(sender:AnyObject){ for var i=0; i<10; i++ { print("NSThread 1 running \(i)") sleep(1) if i == 2 { thread2?.start() //启动线程2 //本线程(thread1)锁定 condition1.lock() condition1.wait() condition1.unlock() } } print("NSThread 1 over") //线程2激活 condition2.signal() } //方法2 func method2(sender:AnyObject){ for var i=0; i<10; i++ { print("NSThread 2 running \(i)") sleep(1) if i == 2 { //线程1激活 condition1.signal() //本线程(thread2)锁定 condition2.lock() condition2.wait() condition2.unlock() } } print("NSThread 2 over") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }} |
输出结果:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
NSThread 1 running 0NSThread 1 running 1NSThread 1 running 2NSThread 2 running 0NSThread 2 running 1NSThread 2 running 2NSThread 1 running 3NSThread 1 running 4NSThread 1 running 5NSThread 1 running 6NSThread 1 running 7NSThread 1 running 8NSThread 1 running 9NSThread 1 overNSThread 2 running 3NSThread 2 running 4NSThread 2 running 5NSThread 2 running 6NSThread 2 running 7NSThread 2 running 8NSThread 2 running 9NSThread 2 over |
Swift - 多线程实现方式(1) - NSThread的更多相关文章
- Swift - 多线程实现方式(2) - NSOperation和NSOperationQueue
1,Swift继续使用Object-C原有的一套线程,包括三种多线程编程技术: (1)NSThread (2)Cocoa NSOperation(NSOperation和NSOperationQueu ...
- Swift - 多线程实现方式(3) - Grand Central Dispatch(GCD)
1,Swift继续使用Object-C原有的一套线程,包括三种多线程编程技术: (1)NSThread (2)Cocoa NSOperation(NSOperation和NSOperationQueu ...
- iOS中的几种锁的总结,三种开启多线程的方式(GCD、NSOperation、NSThread)
学习内容 欢迎关注我的iOS学习总结--每天学一点iOS:https://github.com/practiceqian/one-day-one-iOS-summary OC中的几种锁 为什么要引入锁 ...
- iOS开发Swift篇(02) NSThread线程相关简单说明
iOS开发Swift篇(02) NSThread线程相关简单说明 一 说明 1)关于多线程部分的理论知识和OC实现,在之前的博文中已经写明,所以这里不再说明. 2)该文仅仅简单讲解NSThread在s ...
- 多线程(一)NSThread
iOS中多线程的实现方案: 技术 语言 线程生命周期 使用频率 pthread C 程序员自行管理 几乎不用 NSthread OC 程序员自行管理 偶尔使用 GCD C 自动管理 经常使用 NSOp ...
- iOS多线程编程技术之NSThread、Cocoa NSOperation、GCD
原文出处: 容芳志的博客 简介iOS有三种多线程编程的技术,分别是:(一)NSThread(二)Cocoa NSOperation(三)GCD(全称:Grand Central Dispatch) 这 ...
- java中创建多线程的方式
在java中比较常用的有三种创建多线程的方式. 方式一:继承Thread类,要重写run方法. 在MyThread类 public class MyThread extends Thread { @O ...
- 实现多线程的方式之实现Callable接口
package com.hls.juc; import java.util.concurrent.Callable;import java.util.concurrent.ExecutionExcep ...
- Java基础加强之并发(二)常用的多线程实现方式
概述 常用的多线程实现方式有2种: 1. 继承Thread类 2. 实现Runnable接口 之所以说是常用的,是因为通过还可以通过JUC(java.util.concurrent)包中的线程池来实现 ...
随机推荐
- centos下yum安装crontab+mysql自动备份
参考博文: centos下yum安装crontab yum install vixie-cron crontabs //安装 chkconfig crond on ...
- SQLite数据转换成sql server数据
需要将SQLite数据库里的数据导入到SQL Server,在网上搜了好久,没有找到一个方便实用的方法. 经过本人的细心琢磨实验,终于让我给找到一好的方法:使用CSV文件作为介质来做转换.现在记录下来 ...
- python中multiprocessing.pool函数介绍_正在拉磨_新浪博客
python中multiprocessing.pool函数介绍_正在拉磨_新浪博客 python中multiprocessing.pool函数介绍 (2010-06-10 03:46:5 ...
- 黑马程序员 ---> 正则表达式
--------------- ASP.Net+Android+IO开发S..Net培训.期待与您交流! --------------- 正则表达式 一.概述: 1.概念:符合一定规则的表达式. 2. ...
- ONOS系统架构演进,实现高可用性解决方案
上一篇文章<ONOS高可用性和可扩展性实现初探>讲到了ONOS系统架构在高可用.可扩展方面技术概况,提到了系统在分布式集群中怎样保证数据的一致性.在数据终于一致性方面,ONOS採用了Gos ...
- mysql入门记录
mysql -h localhost(or ID) -u root -p show databases; create database <数据库名>: drop database < ...
- QThread 与 QObject的关系(QObject可以用于多线程,可以发送信号调用存在于其他线程的slot函数,但GUI类不可重入)
QThread 继承 QObject..它可以发送started和finished信号,也提供了一些slot函数. QObject.可以用于多线程,可以发送信号调用存在于其他线程的slot函数,也可以 ...
- Ajax的三种实现及JSON解析
本文为学习笔记,属新手文章,欢迎指教!! 本文主要是比较三种实现Ajax的方式,为以后的学习开个头. 准备: 1. prototype.js 2. jquery1.3.2.min.js 3. j ...
- perl 面向对象 new方法
[root@wx03 test]# cat Scan.pm package Scan; sub new{ my $class = shift; my $self={ 'a'=>11, 'b'=& ...
- httplib,urllib和urllib2
一.httplib实现了HTTP和HTTPS的客户端协议,一般不直接使用,在python更高层的封装模块中(urllib,urllib2)使用了它的http实现. import httplib con ...