-(void) Viewdidload {
[super viewdidload];
_imgview = [[ Uiimageview alloc : self . View Bounds
_imgview. Contentmode = Uiviewcontentmodescaleaspectfit;
[Self. View addsubview:_imgview];
Additional setup after loading the view, typically from a nib.
Method 1
Nsthread *thread = [[Nsthread alloc]initwithtarget:self selector: @selector (test) Object:nil];
[Thread start];
//
Method 2
[Self performselectorinbackground: @selector (Test) Withobject:nil];
Method 3
[Nsthread detachnewthreadselector: @selector (Test) totarget:self Withobject:nil];
method 4 Nsoperationqueue is an operation queue or thread pool
nsoperationqueue *queue = [[nsoperationqueue alloc]init] ;
//queue.maxconcurrentoperationcount = 1;
[Queue addoperationwithblock:^{// adds an action to the queue
[Self test];
// }];
method 5 do not go directly to create the Nsoperation object
nsinvocationoperation *opertion = [[ nsinvocationoperation " initwithtarget : self : @selector (test) object : nil
opertion.queuepriority = Nsoperationqueuepriorityverylow;
//
Nsinvocationoperation *opertion2 = [[nsinvocationoperation alloc]initwithtarget:self selector: @selector (test1) Object:nil];
opertion2.queuepriority = Nsoperationqueuepriorityveryhigh;
//
// method 6
Threadoperation *threadoperation = [[Threadoperation alloc]init];
[Queue addoperation:threadoperation];
[Queue addoperation: opertion];
[Queue Addoperation:opertion2];
BOOL bool1 = [[Nsthread currentthread]ismainthread];
NSLog (@ "Bool1 is%d", bool1);
for (int i = 0; i <; i + +) {
NSLog (@ "-----------------main%d", i);
// }
}
-(void) test{
@autoreleasepool {
//[Nsthread sleepfortimeinterval:5];
BOOL bool2 = [[Nsthread currentthread]ismainthread];
NSLog (@ "Bool2 is%d", bool2);
for (int i = 0; i <; i + +) {
NSLog (@ "-----------------test%d", i);
// }
[Nstimer scheduledtimerwithtimeinterval:1 target:self selector: @selector (run:) Userinfo:nil Repeats:yes];
Nstimer *timer = [Nstimer timerwithtimeinterval:1 target:self selector: @selector (run:) Userinfo:nil Repeats:yes];
[[Nsrunloop Currentrunloop] Addtimer:timer Formode:nsdefaultrunloopmode];
//
[[Nsrunloop Currentrunloop]run];
/*
http://d.hiphotos.baidu.com/image/w%3D310/sign=897718c6bd096b63811958513c328733/ Ac345982b2b7d0a28fc5db74c8ef76094a369acf.jpg
*/
//NSLog (@ "1");
nsstring *imgurl = @ "Http://g.hiphotos.baidu.com/image/w%3D310/sign=2c97ee80d03f8794d3 Ff4e2fe21a0ead/f636afc379310a550dcbd3ccb34543a98226101e.jpg ";
nsdata *data = [nsdata datawithcontentsofurl: [NSURL urlwithstring: Imgurl]];
//_imgview.image = [UIImage imagewithdata:data];
[ self performselectoronmainthread:@selector(show:) Withobject:d ata waituntildone:NO];
}
}
-(void) Run: (nstimer *) timer{
}
-(void) Show: (nsdata *) data{
_imgview. image = [UIImage imagewithdata:d ATA];
}
-(void) test1{
[Nsthread Sleepfortimeinterval:5];
BOOL bool2 = [[Nsthread currentthread]ismainthread];
NSLog (@ "Bool2 is%d", bool2);
for (int i = 0; i <; i + +) {
NSLog (@ "-----------------test111111%d", i);
// }
//}
Multi-threaded creation in the UI and asynchronous loading of timers and pictures