asynchronous ( closure closure , object[] _params , int _nbOfThreads , string _id ) : threadOrVoid
Call a closure in another thread.
If you are using nbOfThreads, the thread number is limited and future calls will be put in a FIFO stack. If you use nbOfThreads you need to assignate an id to the operation.
Be careful to use local variables in your closure.
If you are using nbOfThreads and id, avoid to use lambda closure. Give a name to your closure and use this name in asynchronous.
To fully understand parallelisation, please read this article.
This function returns the thread if _nbOfThreads is null, else it returns nothing.
If you want to indicate the method in asynchronous "by variable", it is recommanded to use a global closure:
Example
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",
{->
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)
for(i=0;i<10;i++)
{
asynchronous({->
console("Hey!")
sleep(2000)
}, null, 2, "test")
}
See also
asynchronousWaitgetOrCreateThreadObject
parallelize
synchronize
waitDelay
Parameters
closure
The closure to execute in a separate thread.
_params (optional)
Parameters to pass to the closure.
_nbOfThreads (optional)
Maximum number of thread to run operations.
_id (optional)
What you want. If you are using _nbOfThreads you need to chose a unique id.