1 #import "TableViewController.h" 2 3 @interface TableViewController () 4 5 @end 6 7 @implementation TableViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 12 } 13 14 - (void)didReceiveMemoryWarning { 15 [super didReceiveMemoryWarning]; 16 // Dispose of any resources that can be recreated. 17 } 18 19 #pragma mark - Table view data source 20 21 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 22 return 1; 23 } 24 25 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 26 return 100; 27 } 28 29 30 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 31 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; 32 //NSThread 线程对象,轻量级的多线程方式 33 //手动创建,手动启动线程 34 NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(loopIn) object:nil]; 35 //执行 36 [thread start]; 37 //取消,线程启动前可以取消, 38 [thread cancel]; 39 40 //自动执行线程 41 // [NSThread detachNewThreadSelector:@selector(loopIn) toTarget:self withObject:nil]; 42 43 return cell; 44 } 45 -(void)loopIn{ 46 47 //手动创建的子线程须添自动释放池,系统的子线程无需添加 48 @autoreleasepool { 49 //线程休眠(秒) 50 [NSThread sleepForTimeInterval:1]; 51 52 //线程状态cancel/finish/excuting 53 //获取当前线程,输出线程状态 54 NSThread *currentThread = [NSThread currentThread]; 55 //当前线程是否取消 56 NSLog(@"取消:%d",[currentThread isCancelled]); 57 //当前线程是否结束 58 NSLog(@"结束:%d",[currentThread isFinished]); 59 //当前线程是否执行 60 NSLog(@"执行:%d",[currentThread isExecuting]); 61 for (int i = 0; i < 100; i++) { 62 printf("%d",i); 63 //线程退出,强制退出当前线程 64 [NSThread exit]; 65 } 66 } 67 //线程休眠(秒) 68 [NSThread sleepForTimeInterval:1]; 69 70 //线程状态cancel/finish/excuting 71 //获取当前线程,输出线程状态 72 NSThread *currentThread = [NSThread currentThread]; 73 //当前线程是否取消 74 NSLog(@"取消:%d",[currentThread isCancelled]); 75 //当前线程是否结束 76 NSLog(@"结束:%d",[currentThread isFinished]); 77 //当前线程是否执行 78 NSLog(@"执行:%d",[currentThread isExecuting]); 79 for (int i = 0; i < 100; i++) { 80 printf("%d",i); 81 //线程退出,强制退出当前线程 82 [NSThread exit]; 83 } 84 85 } 86 87 88 89 @end
1 #import "ViewController.h" 2 3 @interface ViewController () 4 @property (weak, nonatomic) IBOutlet UIImageView *imageView; 5 6 @end 7 8 @implementation ViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 // Do any additional setup after loading the view, typically from a nib. 13 } 14 //返回 15 - (IBAction)back:(UIButton *)sender { 16 17 [self dismissViewControllerAnimated:YES completion:nil]; 18 //下载 19 } 20 - (IBAction)download:(id)sender { 21 //自动线程 22 [NSThread detachNewThreadSelector:@selector(loadImage) toTarget:self withObject:nil]; 23 24 25 } 26 //加载图片 27 -(void)loadImage 28 { 29 NSURL *url = [NSURL URLWithString:@"http://b.hiphotos.baidu.com/zhidao/pic/item/a8ec8a13632762d01650d902a3ec08fa513dc625.jpg"]; 30 NSData *imgData = [NSData dataWithContentsOfURL:url]; 31 32 UIImage *image = [UIImage imageWithData:imgData]; 33 self.imageView.image = image; 34 } 35 36 - (void)didReceiveMemoryWarning { 37 [super didReceiveMemoryWarning]; 38 // Dispose of any resources that can be recreated. 39 } 40 41 @end