文档章节

多线程

JK_Jack
 JK_Jack
发布于 2015/10/19 17:20
字数 220
阅读 1
收藏 0

@interface ViewController ()

@end

@implementation ViewController

- (
void)viewDidLoad {
    [
super viewDidLoad];
   
// Do any additional setup after loading the view, typically from a nib.
}

- (
void)didReceiveMemoryWarning {
    [
super didReceiveMemoryWarning];
   
// Dispose of any resources that can be recreated.
}

- (
IBAction)button:(id)sender {
   
//子线程
//    NSThread *th = [[NSThread alloc] initWithTarget:self selector:@selector(th:) object:nil];
//    [th start];
    [
NSThread detachNewThreadSelector:@selector(th:) toTarget:self withObject:nil];
   
   
//线程互斥
   
//多线程队列
   
//创建队列
   
NSOperationQueue *q = [[NSOperationQueue alloc] init];
   
//在队列里创建添加子线程
    [q
addOperationWithBlock:^{
       
NSLog(@"q=%d",[[NSThread currentThread] isMainThread]);
       
//返回主线程的函数
        [
self performSelectorOnMainThread:@selector(mainThread:) withObject:nil waitUntilDone:YES];

    }];
   
// NSInvocationOperation创建子线程
   
NSInvocationOperation *inv =[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(th:) object:nil];
    [q
addOperation:inv];
   
   
//GCD(c语言)创建线程
   
//1.创建队列
   
dispatch_queue_t queue = dispatch_queue_create("qq", NULL);
   
//2.创建子线程
   
dispatch_async(queue, ^{
       
//返回主线程
       
dispatch_sync(dispatch_get_main_queue(), ^{
           
        });
    });
   
   
   
   
   
}

-(
void)th:(id)object
{
   
NSLog(@"th=%d",[[NSThread currentThread] isMainThread]);
   
NSURL *url = [NSURL URLWithString:BaseUrl];
   
NSURLRequest *request = [NSURLRequest requestWithURL:url];
   
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
   
UIImage *image = [UIImage imageWithData:data];
    [
self performSelectorOnMainThread:@selector(mainThread:) withObject:image waitUntilDone:YES];
}
-(
void)mainThread:(id)object
{
   
self.imageV.image = object;
}

@end

 

© 著作权归作者所有

共有 人打赏支持
JK_Jack
粉丝 0
博文 6
码字总数 2658
作品 0
广州

暂无文章

docker-compose ,docker-stack

1.例子 version: "3"services: php: image: registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5 ports: - "38080:80" networks: - my_php_mysql volum......

chenbaojun
31分钟前
1
0
SQL_Server2000示例数据库NorthWind的分析(转)

SQL_Server2000示例数据库NorthWind的分析 表名:Categories(食品类别表) 表结构: 字段名称 数据类型 长度 允许为空 CategoryID(主键) int 4 否 CategoryName nvarchar 15 否 Description ...

QQZZFT
34分钟前
1
0
laravel 5.5 Session store not set on request.

laravel 5.5 数据存入session,会出现Session store not set on request.错误。查了下laravel 5.5将session放到global middleware中,需要laravel的文件 ./app/Http/Kernel.php中的加上一句:...

MichaelShu
今天
1
0
OpenCV VideoCapture.get()参数详解

param define cv2.VideoCapture.get(0) 视频文件的当前位置(播放)以毫秒为单位 cv2.VideoCapture.get(1) 基于以0开始的被捕获或解码的帧索引 cv2.VideoCapture.get(2) 视频文件的相对位置(...

NateHuang
今天
0
0
java基础知识,小栗子

来操作一下数组.....注意带参数的变长数组的使用. package com.avatus;import java.util.Random;import java.util.Scanner;public class Main { public static void main(St...

Oh_really
今天
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部