资源加载中... loading...

ThreadCondition

条件对象,用于多线程同步。

notify

notify()函数用于唤醒一个正在等待的线程(如果有的话)。只有调用了wait()方法的线程才会被唤醒。

notify()

function consumer(dict, condition) {
    while (true) {
        condition.acquire()
        while (dict.get("array").length == 0) {
            Log(threading.currentThread().name(), "wait()...", ", array:", dict.get("array"))
            condition.wait()
        }
        var arr = dict.get("array")
        var num = arr.shift()
        Log(threading.currentThread().name(), ", num:", num, ", array:", arr, "#FF0000")
        dict.set("array", arr)
        Sleep(1000)
        condition.release()
    }
}

function main() {
    var condition = threading.Condition()
    var dict = threading.Dict()
    dict.set("array", [])
    var t1 = threading.Thread(consumer, dict, condition)
    var t2 = threading.Thread(consumer, dict, condition)
    var t3 = threading.Thread(consumer, dict, condition)
    Sleep(1000)
    var i = 0
    while (true) {
        condition.acquire()
        var msg = ""
        var arr = dict.get("array")
        var randomNum = Math.floor(Math.random() * 5) + 1
        if (arr.length >= 3) {
            condition.notifyAll()
            msg = "notifyAll"
        } else {
            arr.push(i)
            dict.set("array", arr)
            if (randomNum > 3 && arr.length > 0) {
                condition.notify()
                msg = "notify"
            } else {
                msg = "pass"
            }
            i++
        }

        Log(_D(), "randomNum:", randomNum, ", array:", arr, ", msg:", msg)
        condition.release()
        Sleep(1000)
    }
}

使用notify()函数唤醒等待中的线程。

notify()函数会唤醒处于等待队列中的一个线程。

notify()函数唤醒一个线程时,这个线程会重新获取线程锁。

{@fun/Threads/ThreadCondition/notifyAll notifyAll}, {@fun/Threads/ThreadCondition/wait wait}, {@fun/Threads/ThreadCondition/acquire acquire}, {@fun/Threads/ThreadCondition/release release}

notifyAll

notifyAll()函数会唤醒所有正在等待的线程。

notifyAll()

范例可以参考ThreadCondition.notify()章节的内容。

notifyAll()函数逐个唤醒所有等待中的线程,被唤醒的线程重新获取线程锁。

{@fun/Threads/ThreadCondition/notify notify}, {@fun/Threads/ThreadCondition/wait wait}, {@fun/Threads/ThreadCondition/acquire acquire}, {@fun/Threads/ThreadCondition/release release}

wait

wait()函数用于在某些设计的条件下让线程等待。

wait()

范例可以参考ThreadCondition.notify()章节的内容。

wait()函数会释放线程锁,当被唤醒时重新获取线程锁。

{@fun/Threads/ThreadCondition/notify notify}, {@fun/Threads/ThreadCondition/notifyAll notifyAll}, {@fun/Threads/ThreadCondition/acquire acquire}, {@fun/Threads/ThreadCondition/release release}

acquire

acquire()函数用于请求一个线程锁(加锁)。

acquire()

范例可以参考ThreadCondition.notify()章节的内容。

在使用wait()之前需要请求当前条件对象的线程锁(加锁)。

{@fun/Threads/ThreadCondition/notify notify}, {@fun/Threads/ThreadCondition/notifyAll notifyAll}, {@fun/Threads/ThreadCondition/wait wait}, {@fun/Threads/ThreadCondition/release release}

release

release()函数用于释放一个线程锁(解锁)。

release()

范例可以参考ThreadCondition.notify()章节的内容。

在使用wait()之后需要释放当前条件对象的线程锁(解锁)。

{@fun/Threads/ThreadCondition/notify notify}, {@fun/Threads/ThreadCondition/notifyAll notifyAll}, {@fun/Threads/ThreadCondition/wait wait}, {@fun/Threads/ThreadCondition/acquire acquire}

ThreadEvent ThreadDict