Multi-threaded-2

Source: Internet
Author: User

Nsoperation is a base class (abstract class (such as NSObject) that cannot be used directly by a subclass that must use it, which is operated by default on the current thread

Often used in conjunction with Nsoperationqueue (thread queue), which automatically opens up threads for operation when used with thread queues, without manual manipulation (* * * * *). If you only use nsoperation alone you must enable it manually

The Nsoperation system provides two sub-classes of nsinvocationoperation,nsblockoperation.

Single use operation he is synchronous execution

Nsblockoperation the number of operands is greater than 1 will open the thread to perform the operation

Initialization: Nsinvocationoperation

When it is used alone, remember to use manual operation: [Invocation start];

Invocation Implementation Method:

-(void) Act: (ID) sender{

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

, [Nsthread CurrentThread] is which thread

}

Initialize Nsblockoperation:

Add action

Nsblockoperation the number of operands greater than 1 will open the thread to perform the operation [Blockoperation addexecutionblock:^{[Self act:@ "add operation"];}];

Manual operation is also required for use alone:

[Blockoperation start];

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

Do not manually start the thread (* * *)

Initialize Nsoperationqueue

Sets the maximum number of concurrent threads (up to several operations, simultaneous operations) for the thread queue, i.e. if Queque.maxconcurrentoperationcount = 0, then only the main thread is executing

Queque.maxconcurrentoperationcount = 2;

Add Action (no manual action required when adding an action)

[Queque addoperation:invocation]; (when invocation added to Queque, put the previous [invocation start]; Comment out)

Multi-threaded-2

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.