iosでdispatch queueを実現するプログラム

コード:

dispatch_queue_t aQueue,bQueue;

//dispatch_queue_t変数を定義

aQueue=dispatch_queue_create(“firstQueue", NULL);

//dispatch_queue_tオブジェクトを作成
bQueue=dispatch_queue_create(“sencondQueue",NULL);

//非同期aQueueを呼び出す
dispatch_async(aQueue, ^{
for (int i=0; i<30; i++) {
NSLog(@"first aQueue run:%i",i);
[NSThread sleepForTimeInterval:10];
}

});
//非同期bQueueを呼び出す
dispatch_async(bQueue, ^{
for (int i=0; i<30; i++) {
NSLog(@"second aQueue run:%i",i);
[NSThread sleepForTimeInterval:5];
}

});

説明:
A dispatch queue invokes blocks submitted to it serially in FIFO order. A serial queue invokes only one block at a time, but independent queues may each invoke their blocks concurrently with respect to each other.
The global concurrent queues invoke blocks in FIFO order but do not wait for their completion, allowing multiple blocks to be invoked concurrently.
The system manages a pool of threads that process dispatch queues and invoke blocks submitted to them. Conceptually, a dispatch queue may have its own thread of execution, and interaction between queues is highly asynchronous.
Dispatch queues are reference counted via calls to dispatch_retain and dispatch_release. Pending blocks submitted to a queue also hold a reference to the queue until they have finished. Once all references to a queue have been released, the queue will be deallocated by the system

IOS

Posted by arkgame