多线程THread
来源:互联网 发布:柳天捏脸数据 编辑:程序博客网 时间:2024/05/19 20:44
文件1:ViewController.m
#import "ViewController.h"#import "MyThread.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.label.backgroundColor = [UIColor blackColor];
// Do any additional setup after loading the view, typically from a nib.
}
- (void) print
{
NSArray *arr = [NSThread callStackSymbols];// 线程函数地址
NSLog(@"%@",arr);
for (int i = 0 ; i<200; i++) {
NSLog(@"%i",i);
[self.label setText:[NSString stringWithFormat:@"%d",i]];
//不能在子线程中刷新UI
//[self.label performSelectorOnMainThread:@selector(setText:) withObject:[NSString stringWithFormat:@"%d",i] waitUntilDone:YES];
[self performSelectorOnMainThread:@selector(labelText:) withObject:[NSString stringWithFormat:@"%i",i] waitUntilDone:YES];
NSLog(@"%@",[NSThread isMainThread]?@"main":@"sub");// 判断当前线程是否为主线程
if (i>100) {
NSThread *thread = [NSThread currentThread];//返回当前线程
[thread cancel];// 取消操作
NSLog(@"%i",thread.isCancelled);// 是否取消的
}
//使所在的线程暂停
[NSThread sleepForTimeInterval:1.0f];
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2.0f]];
}
}
- (void) labelText:(NSNumber *)value
{
self.label.text = [NSString stringWithFormat:@"%@",value];
self.label.textColor = [UIColor whiteColor];
// if ([value intValue] % 2 ) {
// _label.backgroundColor = [UIColor redColor];
// }
// else {
// _label.backgroundColor = [UIColor greenColor];
// }
self.label.backgroundColor = [UIColor colorWithRed:(int)value/2000 green:1 blue:0 alpha:1];
self.label.textColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1-(int)value/2000];
//退出线程
// [NSThread exit];
//退出程序
// exit(0);
// abort();
}
- (IBAction)didClicked:(id)sender {
NSLog(@"sldskdf");
// [self print];
//创建子线程的几种方式:
[self performSelectorInBackground:@selector(print) withObject:nil];
// [NSThread detachNewThreadSelector:@selector(print) toTarget:self withObject:nil];
// NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(print) object:nil];
// [thread start];
// MyThread *myThread = [[MyThread alloc] init];
// myThread.label = self.label;
// [myThread start];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
文件2:MyThread.h
@interface MyThread : NSThread
@property(nonatomic,weak) UILabel *label;
@end
文件3:MyThread.m
#import "MyThread.h"@implementation MyThread
//线程入口
- (void) main
{
[self print];
}
- (void) print
{
for (int i = 0; i<100; i++) {
NSLog(@"%i",i);
[_label performSelectorOnMainThread:@selector(setText:) withObject:[NSString stringWithFormat:@"%i",i] waitUntilDone:YES];
if (i/2==0) {
[_label performSelectorOnMainThread:@selector(setBackgroundColor:) withObject:[UIColor whiteColor] waitUntilDone:YES];
}
else
{
[_label performSelectorOnMainThread:@selector(setBackgroundColor:) withObject:[UIColor redColor] waitUntilDone:YES];
}
}
}
@end
0 0
- 多线程Thread
- 多线程 Thread
- 多线程Thread
- 多线程-thread
- Thread-多线程
- 多线程Thread
- 多线程THread
- Thread多线程
- Thread多线程
- 多线程Thread类创建多线程
- java Thread 多线程
- C#多线程之Thread
- Thread Runnable 多线程
- C#多线程之Thread
- 多线程(二) Thread
- Java Thread 多线程 介绍
- Posix thread 多线程编程
- Java Thread多线程
- android ListView-选中项固定在某一项
- __autoload与spl_autoload_register
- 秀外慧中的springMVC(六)---输入和输出使用xml格式方式
- quick的事件
- java对象序列化和反序列化必须要注意
- 多线程THread
- 利用开源组件commons-fileupload上传图片文件(1)
- 【Git学习笔记】将文件放入仓库只需两步add和commit
- shape的使用总结
- extern
- jFinal中报对应模型不存在的错误(The Table mapping of model: demo.User not exists)
- How to make Universal Static library (.a file) in iOs using xCode
- Java的运行时数据存储机制
- 第9周 项目6-6 “都要学C”