Operation (Swift)

Source: Internet
Author: User

Introduction:

Nsoperation is an abstract class and does not have the ability to encapsulate operations, it must use its subclasses;

blockoperation:

Operations are performed asynchronously as long as the blockoperation encapsulated operand > 1, but no unlimited creation of threads

Let Blockope = blockoperation ()        = . Veryhigh        Blockope.addexecutionblock {            print ("  blockoperation executed ", Thread.current)        }        blockope.addexecutionblock {            Print ("BlockOperation2 executed ", Thread.current)        }        Blockope.start ()  / /Start execution         / // Cancel 

Results:

Nsoperationqueue Creating a queue

1.1 Gets the home column and adds operations to the queue, the home column is a serial queue, regardless of whether the maximum number of concurrent sets is executed on the current thread, and no new threads are created

1    Let queue = operationqueue.main2        queue.addoperation {3              for inch 0... 50000 {4                 print ("OperationQueue1 executed ", Thread.current)  5            }6         }

Results:

2.1 Simultaneous serial and concurrent functions, which are concurrent by default, can be manually set to serial queue, by setting the maximum concurrency Number property to change

1 c func racstest () {2Let queue =Operationqueue ()3Queue.maxconcurrentoperationcount =Ten4 queue.addoperation {5              for_inch 0...50000{6Print"OperationQueue1 executed the", Thread.current)7             }8         }9         //Addoperation has already executed the Start method internally .

Results:

Operation (Swift)

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.