Yasmin
Yasmin

Reputation: 951

cannot cast java.util.concurrent.ForkJoinWorkerThread into java.lang.Thread- Complex structure

I understand the concept of fork/join, but almost all resources over the internet use, Fibonacci as an example, but my scenario is more complex. I sketched the program, and I have an exception as commented in the below code..

Class Test
{

  public static void main(String[] args) 
{

    ForkJoinPool p= new ForkJoinPool(5);
    p.invoke(new Train());
}
}


Class Train extends RecursiveAction
{
public Train(int d, int n)
{
    //some intialization
}
public Train()
{
    t= new Train[5];
    new Vec().run_Vec(t);
}
@Override
protected void compute() {

        for(int i= 1; i< 8; i++)
        {
            // x, and y are predefined
            temp[x][y] = some calculation;

        }

}

}
class Vec
{
    public void run_Vec(Train[] t) {

        for (int i = 0; i < 5; i++) {
            t[i] = new Train(i*4, i/2);
            t[i].fork(); // error java.lang.Thread cannot be cast to   java.util.concurrent.ForkJoinWorkerThread
            }
        for (int i = 0; i < 5; i++) {

            t[i].join();
        }

    }

}
}

Upvotes: 0

Views: 941

Answers (2)

Markus
Markus

Reputation: 773

Based on this Java article I made the following code snippet: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/RecursiveAction.html

You should only call fork (spawn) within a "running" Thread. This means you must pass on the Train array within the compute method:

package ...;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;

class ConcurrentTest {

public static void main(String[] args) {
    ForkJoinPool p= new ForkJoinPool(5);
    p.invoke(new Train());
}


public static class Train extends RecursiveAction {
    private Train[] t = null;
    public Train(int d, int n) {
        //some code
    }

    public Train() {
        t= new Train[5];
    }

    @Override
    protected void compute() {
        if(t != null) {
            new Vec().run_Vec(t);
            for(int i= 1; i< 8; i++) {
                System.out.println("Test.Train.compute(): " + i);   
            }
        }
    }
}


public static class Vec
{
    public void run_Vec(Train[] t) {
        for (int i = 0; i < 5; i++) {
            t[i] = new Train(i*4, i/2);
            System.out.println("Clazz: " + t[i].getClass());
            t[i].fork(); // error java.lang.Thread cannot be cast to   java.util.concurrent.ForkJoinWorkerThread
        }
        for (int i = 0; i < 5; i++) {
            t[i].join();
        }
    }

}
}

Upvotes: 0

anonymous
anonymous

Reputation: 1317

I think your problem is due to calling fork() from the main thread. When you call p.invoke(new Train()), your default train constructor actually calls the run_vec() and tries to fork(). Upon reading the javadocs, there are examples that fork() is called within compute(). You need to be calling fork from a thread started by p.invoke().

Upvotes: 1

Related Questions