The resource loading... loading...

ThreadCondition

Condition object, used for multi-thread synchronization.

notify

The notify() function is used to wake up a waiting thread (if any). Only threads that have called the wait() method will be woken up.

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)
    }
}

Use the notify() function to wake up the waiting thread.

The notify() function wakes up a thread in the waiting queue.

When the notify() function wakes up a thread, the thread will reacquire the thread lock.

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

notifyAll

The notifyAll() function wakes up all waiting threads.

notifyAll()

Please refer to the ThreadCondition.notify() section for examples.

The notifyAll() function wakes up all waiting threads one by one, and the awakened threads reacquire the thread lock.

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

wait

The wait() function is used to make a thread wait under certain designed conditions.

wait()

Please refer to the ThreadCondition.notify() section for examples.

The wait() function releases the thread lock and reacquires the thread lock when woken up.

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

acquire

The acquire() function is used to request a thread lock (lock).

acquire()

Please refer to the ThreadCondition.notify() section for examples.

Before using wait(), you need to request the thread lock (lock) of the current condition object.

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

release

The release() function is used to release a thread lock (unlock).

release()

Please refer to the ThreadCondition.notify() section for examples.

After using wait(), we need to release the thread lock (unlock) of the current condition object.

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

ThreadEvent ThreadDict