English 中文(简体)
快速应急:调查背景变化
原标题:Swift Concurrency: Investigating Context Changes Triggered by Function Calls in Tasks

I m 曲解为什么Task CTask D? 与Task ATask B不同,在主线上不执行。 我理解,任务由父母继承,在这种情况下,他们都与父母相同。 可以假定,电话功能产生detastal>。 任务?

struct ThreadTestView: View {
    var body: some View {
        Text("Test Thread")
            .onAppear {
                // Task A
                print("Task A - (threadInfo())")
                
                // Task B
                Task {
                    print("Task B - (threadInfo())")
                }
                
                // Task C
                printInfo("Task C")
                
                // Task D
                Task {
                    printInfo("Task D")
                }
            }
    }
    
    func printInfo(_ source: String) {
        Task {
            print("(source) - (threadInfo())")
        }
        // detached
        Task.detached {
            print("(source) - detached (threadInfo())")
        }
    }
    
    func threadInfo() -> String {
        "isMain:(Thread.isMainThread) - (Thread.current)"
    }
}

#Preview {
    ThreadTestView()
}

青少年产出:

Task A - isMain:true - <_NSMainThread: 0x280694040>{number = 1, name = main}
Task C - isMain:false - <NSThread: 0x2806c6b00>{number = 8, name = (null)}
Task C - detached isMain:false - <NSThread: 0x2806c4580>{number = 5, name = (null)}
Task B - isMain:true - <_NSMainThread: 0x280694040>{number = 1, name = main}
Task D - isMain:false - <NSThread: 0x2806c6b00>{number = 8, name = (null)}
Task D - detached isMain:false - <NSThread: 0x2806c6b00>{number = 8, name = (null)}
问题回答

Huge disapper: 我仍在填补我自己在迅速理解应急方面的空白,因此,我高兴地在以下任何发言中被证明是错误的。

我认为,这里重要的出发点是,“主线”与“主要角色”并不相同。 两者之间存在若干差异,但一个显著的差别是,行为者是一个集思广益的构造,read子大多是一次性的。 仅仅因为法典在主线上运行,并不意味着“主角-异构体”。 查阅https://developer.apple.com/documentation/swiftui/view/onappear(perform:)”rel=“nofollow noreferer”>onAppear(perform: 方法,见<代码>perform。 关闭并不包括@MainActor的注解,因此它不是主角,尽管我们完全可以相信,它将被称作主线。 因此,当你在关闭后立即发出<代码>Task时——尽管事实上<代码>Task.init<>/code>继承了现有行为者——关闭本身不是行为者,因此没有行为者继承。 起诉人可以自由安排这项任务,看看看合作社的校友,甚至看看看上去的主线。 注明<代码>印本/编码>的功能@MainActor,以了解情况如何变化。

A和B处于主线,因为 载于,与主要行为体隔绝(因此在主线上运行)。

C和D并不在主线上,因为Info不是角色-异构体,因此,尽管如此,你却主动提出不结构的一致意见,但由此产生的任务要么不是行为体-异构体。 因此,它从合作社的校对池,而不是主线。


如果Info方法与主要行为者分离(或明确附在@MainActor前)。 故事本身,或属于与主要行为者及其所有成员隔离的一类成员,将会改变。 然后将其<代码>Task{......}与同一行为体隔离, 任务(......}仍然不能孤立于任何行为者,因此会从合作线库中随机穿透。





相关问题
Need help with web application settings [closed]

Pls give me the solutions for this two problem, which i was asked recently in interview. Prob1: Suppose I have application with 10 admin, so they can change the data anytime their having own userid ...

AutoResetEvent, ManualResetEvent vs Monitor

Lets say I have to orchestrate a synchronization algorithm in .Net 3.5 SP1 and any of the synchronization primitives listed in the title fit perfectly for the task. From a performance perspective, is ...

PHP and Concurrency

I ve been doing some web development work in PHP recently which has led me to study up on the language in general. So far I have not needed to use it to interact with a database, but I know it ...

UI And TcpClient Issue in vb.net

I m having some problems with a small ircbot i m writing. Basically I connect to the server using a tcpclient in a seperate class, which also runs on its own thread. I want to display the server text ...

Prevent Concurrent Editing of a List Item

In Sharepoint MOSS multiple users can edit the same item in a sharepoint list at the same time…the first person to save their edit “wins”. Is there a way to prevent this, to lock the list item while ...

How to properly catch RuntimeExceptions from Executors?

Say that I have the following code: ExecutorService executor = Executors.newSingleThreadExecutor(); executor.execute(myRunnable); Now, if myRunnable throws a RuntimeExcpetion, how can I catch it? ...

Concurrent modification whilst traversing a ruby Hash

Suppose you had this: def wipeProduct(hash, nameToDelete) hash.each do |i| key = i[0] productName = i[1].first hash.delete(key) if productName==nameToDelete end end I m not sure it ...

热门标签