Câu hỏi phỏng vấn Swift
Câu hỏi

Explain usage of Concurrent vs Seri...

Câu trả lời

Concurrent vs Serial Queues with Async and Sync Blocks in Swift

In Swift, concurrency and task management are handled using DispatchQueue, which can be either serial or concurrent. Understanding the differences between these types of queues and how they interact with synchronous (sync) and asynchronous (async) blocks is crucial for efficient and effective code execution.

Serial Queues

Definition:
A serial queue executes tasks one at a time in the order they are added. This ensures that only one task runs at any given moment, making it ideal for tasks that must be performed sequentially.

Usage:

  • Synchronous Execution (sync): When a task is added to a serial queue using sync, the calling thread is blocked until the task completes. This can lead to deadlocks if not managed carefully, especially if the sync block tries to add another sync block to the same queue.

    swift Copy
    let serialQueue = DispatchQueue(label: "com.example.serial")
    
    serialQueue.sync {
        print("Task 1")
        serialQueue.sync {
            print("Task 2") // This will cause a deadlock
        }
    }
  • Asynchronous Execution (async): Adding a task using async does not block the calling thread. The tasks are executed in the order they are added, but the calling thread can continue executing other code.

    swift Copy
    let serialQueue = DispatchQueue(label: "com.example.serial")
    
    serialQueue.async {
        print("Task 1")
        serialQueue.async {
            print("Task 2")
        }
    }
    // Output: Task 1, Task 2

Concurrent Queues

Definition:
A concurrent queue allows multiple tasks to run simultaneously. Tasks start in the order they are added but can finish in any order, depending on system conditions and task complexity.

Usage:

  • Synchronous Execution (sync): Similar to serial queues, using sync on a concurrent queue blocks the calling thread until the task completes. However, since tasks can run concurrently, this is less common and can lead to complex synchronization issues.

    swift Copy
    let concurrentQueue = DispatchQueue(label: "com.example.concurrent", attributes: .concurrent)
    
    concurrentQueue.sync {
        print("Task 1")
        concurrentQueue.sync {
            print("Task 2")
        }
    }
    // Output: Task 1, Task 2 (but can lead to complex issues)
  • Asynchronous Execution (async): This is the most common use case for concurrent queues. Tasks are added to the queue and executed concurrently, allowing for efficient use of system resources and faster execution times.

    swift Copy
    let concurrentQueue = Disp...
expert

expert

Gợi ý câu hỏi phỏng vấn

expert

What’s the difference between init?() and init() ?

senior

What is the autoclosure attribute and when to use it?

middle

Explain the difference between DispatchQueue.main.async and DispatchQueue.main.sync ?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào