Operation (Custom multithreading)

Source: Internet
Author: User

//

Viewcontroller.m

ui-no-25

//

Created by Bruce on 15/8/27.

Copyright (c) 2015 Bruce. All rights reserved.

//

#import "ViewController.h"

@interface Viewcontroller ()

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

You cannot use operation directly because there is no way to manipulate it directly, just a base class (abstract class)

Nsinvocationoperation *invocation = [[nsinvocationoperation alloc]initwithtarget:self selector: @selector (act:) object:@ "Invocation"];

If you use operation alone you must start manually

[Invocation start];

Operation default on current thread execution (synchronous execution)

Nsinvocationoperation *invocation1 = [[nsinvocationoperation alloc]initwithtarget:self selector: @selector (act:) object:@ "Invocation1"];

If you use operation alone you must start manually

Nsinvocationoperation *invocation2 = [[nsinvocationoperation alloc]initwithtarget:self selector: @selector (act:) object:@ "Invocation2"];

Nsinvocationoperation *invocation3 = [[nsinvocationoperation alloc]initwithtarget:self selector: @selector (act:) object:@ "Invocation3"];

Nsinvocationoperation *invocation4 = [[nsinvocationoperation alloc]initwithtarget:self selector: @selector (act:) object:@ "Invocation4"];

Nsoperationqueue do not manually start the thread so comment out start

[Invocation1 start];

Operation default on current thread execution (synchronous execution)

Nsblockoperation *blockoperation = [Nsblockoperation blockoperationwithblock:^{

Specific methods of implementation

[Self act:@ "blockoperation"];

}];

Add action

Nsblockoperation the number of operands is greater than 1 will open thread execution

[Blockoperation addexecutionblock:^{

[Self act:@ "blockOperation2"];

}];

[Blockoperation start];

Operation default on current thread execution (synchronous execution)

Nsoperationqueue (thread queue) management thread (default will open thread to execute)

Nsoperationqueue do not manually start the thread

Asynchronous execution

Nsoperationqueue *queue = [[Nsoperationqueue alloc]init];

Sets the maximum number of concurrent threads in a queue (can allow several operations to execute concurrently)

Queue.maxconcurrentoperationcount = 1;

[Queue addoperation:invocation];

[Queue Addoperation:invocation1];

[Queue Addoperation:invocation2];

[Queue Addoperation:invocation3];

[Queue Addoperation:invocation4];

[Queue addoperation:blockoperation];

}

-(void) Act: (ID) sender

{

NSLog (@ "%@\n%@", [Nsthread Currentthread],sender);

}

-(void) didreceivememorywarning {

[Super didreceivememorywarning];

Dispose of any resources the can be recreated.

}

@end

Operation (Custom multithreading)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.