javainheritancefork-joinforkjoinpoolrecursivetask

Abstract class inheriting from RecursiveTask


I'm having problems to resolve an issue. First I have an abstract class inheriting from RecursiveTask:

public abstract class GeneratorTaskBase<T, U extends RecursiveTask<T>> extends RecursiveTask<T> {
        @Override
protected T compute() {
    LOG.debug("Computing: start={}, end={}", start, end);
    if (end - start <= THRESHOLD) {
        try {
            return process();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } else {
        final int mid = start + (end - start) / 2;
        final U leftTask = getTask(start, mid);
        final U rightTask = getTask(mid, end);

        leftTask.fork();
        rightTask.fork();

        final T leftResult = leftTask.join();
        final T rightResult = rightTask.join();

        return getResult(leftResult, rightResult);
    }
}

    protected abstract T getResult(T leftResult, T rightResult);

    protected abstract T process() throws Exception;

    protected abstract U getTask(final int start, final int end);

    protected abstract String generate();

}

A subclass

public class SqlGenerator extends GeneratorTaskBase<String, SqlGenerator> {


@Override
public String generate() {
    this.end = this.files.size();
    return this.invoke();
}

@Override
protected SqlGenerator getTask(final int start, final int end) {
    return new SqlGenerator(this.path).files(files).start(start).end(end);
}


}

I'm making the call for the task like this:

public final class CsvAsSqlDataProcessor implements
    DataProcessor<String, FileInput> 
@Override
public void process(FileInput input) {
    final String fileName = input.source().getName();
    final String directory = input.getDirectory();

    LOG.debug("directory: {}", directory);

    try {
        final CSVReader reader = new CSVReader(new FileReader(directory
                .concat(File.separator).concat(fileName)),
                SEMICOLON_DELIMETER);

        final List<String[]> rows = reader.readAll();

        reader.close();

        fullInsertStatement = new InsertSqlGenerator(rows, input.source())
                .generate();

    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

When I run it I'm getting the exception:

Exception in thread "main" java.lang.ClassCastException: java.lang.Thread     cannot be cast to java.util.concurrent.ForkJoinWorkerThread
    at java.util.concurrent.ForkJoinTask.fork(ForkJoinTask.java:622)
    at com.bosch.mome.importer.batch.export.sql.GeneratorTaskBase.compute(GeneratorTaskBase.java:40)
    at java.util.concurrent.RecursiveTask.exec(RecursiveTask.java:93)
    at java.util.concurrent.ForkJoinTask.doInvoke(ForkJoinTask.java:377)
    at java.util.concurrent.ForkJoinTask.invoke(ForkJoinTask.java:654)
    at com.bosch.mome.importer.batch.export.sql.SqlGenerator.generate(SqlGenerator.java:69)

Can anybody give a hint about what I'm doing wrong?


Solution

  • I solved like this:

    @Override
    public String generate() {
        this.end = this.files.size();
        ForkJoinPool pool = new ForkJoinPool();
        pool.execute(this);
        return this.join(); 
    }