synchronize ( object/string locker , closure method ) : void
Ensures the synchronicity of a task in relation to others, and allows to not execute in different threads some dependant actions at the same time. Example: you search a brand on a site, if it not exist, the script create it. But if 2 thread do that at the same time, there is a risk of duplicated brand.
Example
synchronize("brand",{->
....actions to add a brand...
})
def grade_student_class_1 = [10,12,11,9,8,0,1,20,19,14]
def grade_student_class_2 = [2,2,3,9,5,13,1]
sum_grade = 0
def datas_for_average = // Closure wich does the sum of class grades
{def tab_grade->
for(def grade in tab_grade)
{
synchronize("sum_grade", // Only one thread can access this at the same time, others wait that the thread in go out the closure
{->
sum_grade+=grade
})
}
}
asynchronous(datas_for_average, [grade_student_class_1], 2, "datas_for_class") // Start the thread for the sum of class 1 grades
asynchronous(datas_for_average, [grade_student_class_2], 2, "datas_for_class") // Start the thread for the sum of class 2 grades
asynchronousWait("datas_for_class")
average = sum_grade /(count(grade_student_class_1)+count(grade_student_class_2))
console(average)
Note on multi-thread operations
All Grimport variables are global by default, use "def" to make local variables in your method. Use synchronize to manage shared variables between thread (read and write operations).Parameters
locker
An object or a string (ex:brand,supplier,category,feature,combination,global,translate,api,disk) which lock the synchronize
method
The function which is synchronized