PeraturanCondition()
fungsi digunakan untuk membuat objek variabel kondisi, yang digunakan untuk mencapai sinkronisasi dan komunikasi antara thread dalam lingkungan serentak multi-threaded.Condition()
, sebuah thread dapat menunggu ketika kondisi tertentu tidak terpenuhi sampai thread lain memberitahunya bahwa kondisi telah terpenuhi.
PeraturanCondition()
fungsi mengembalikan aThreadCondition
object.
ThreadCondition
objek
Kondisi
function consumer(productionQuantity, dict, condition) {
for (var i = 0; i < productionQuantity; i++) {
condition.acquire()
while (dict.get("array").length == 0) {
condition.wait()
}
var arr = dict.get("array")
var count = arr.shift()
dict.set("array", arr)
Log("consumer:", count, ", array:", arr)
condition.release()
Sleep(1000)
}
}
function producer(productionQuantity, dict, condition) {
for (var i = 0; i < productionQuantity; i++) {
condition.acquire()
var arr = dict.get("array")
arr.push(i)
dict.set("array", arr)
Log("producer:", i, ", array:", arr)
condition.notify()
condition.release()
Sleep(1000)
}
}
function main() {
var dict = threading.Dict()
dict.set("array", [])
var condition = threading.Condition()
var productionQuantity = 10
var producerThread = threading.Thread(producer, productionQuantity, dict, condition)
var consumerThread = threading.Thread(consumer, productionQuantity, dict, condition)
consumerThread.join()
producerThread.join()
}
Dua thread bersamaan mengakses sumber daya yang sama.
Sistem backtesting tidak menerapkan fungsi ini, hanya mendefinisikannya.
{@fun/Threads/getThread getThread}, {@fun/Threads/threading/mainThread mainThread}, {@fun/Threads/threading/currentThread currentThread}, {@fun/Threads/threading/Lock Lock}, {@fun/Threads/threading/Thread Thread}, {@fun/Threads/threading/event}, {@fun/Threads/threads/threading/Dict Dict}, {@fun/Threads/threading/pending pending}, {@fun/Threads/threading/eventLoop}
Kunci Peristiwa