I am fairly new with Java Threads, I normally use C with it comes to parallelization. To parallelize algorithm that has the same pattern as the one it follows:
void traverse(node* p)
{
if (p->left)
#pragma omp task // p is firstprivate by default
traverse(p->left);
if (p->right)
#pragma omp task // p is firstprivate by default
traverse(p->right);
}
I would use the task directive of openMP, for example.
Task Description
When a thread encounters a task construct, a task is generated from the code for the associated structured block. The encountering thread may immediately execute the task, or defer its execution. In the latter case, any thread in the team may be assigned the task. Completion of the task can be guaranteed using task synchronization constructs. A task construct may be nested inside an outer task, but the task region of the inner task is not a part of the task region of the outer task.
My question is:
How I could implement this same idea (task) with Java Threads?
The pragmas of OpenMP do make parallelization a little easier.
In Java, you would first need to create a class that implements Runnable.
- example: public Class Traverse implements Runnable
You then just neew to create the class and call 'run' to start the thread.
private void traverse(node p)
{
Traverse t = null;
if (p.left)
t = new Traverse(p.left);
if (p->right)
t = new Traverse(p.right)
t.run(); // start thread. this call will not wait for run to finishes
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With