从RecursiveTask继承的抽象类

时间:2015-02-03 11:02:28

标签: java inheritance fork-join forkjoinpool recursivetask

我在解决问题时遇到问题。首先,我有一个继承自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();

}

子类

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);
}


}

我打电话给这样的任务:

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);
    }
}

当我运行它时,我得到了例外:

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)

任何人都可以暗示我做错了吗?

1 个答案:

答案 0 :(得分:1)

我这样解决了:

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