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)