parallelize fast-serialisation java

391 views Asked by At

It's the first time, that I'm working with Threads in Java. Im trying to parallelize fast-serialisation. But I'm getting following error:

Exception in thread "pool-1-thread-1" java.lang.RuntimeException: Class org.nustaq.serialization.FSTObjectOutput does not implement Serializable or externalizable

So how do I parallelize fast-serialisation correctly? Where is my mistake?

Here is my runnable Class:

public class RunnableTestClass implements Serializable, Runnable {

FSTObjectOutput out;
ObjectOutputStream stream;
private long a,b,c,d,e,f,g,h;
public RunnableTestClass(long i, ThreadLocal<FSTConfiguration> conf) throws Exception {
    a = i;
    //some more
    stream = new ObjectOutputStream(new FileOutputStream("out/FST.ser_parallel"));
    out = conf.get().getObjectOutput(stream);
}

@Override
public void run() {
    try {
        out.writeObject(this, TestClass.class);
        out.flush();
        stream.close();
    } catch (IOException e) {
        System.out.println(e);
    }
}

And here my main:

public class Test {

public static FSTConfiguration[] configurations = new FSTConfiguration[4];
public static ThreadLocal<FSTConfiguration> conf = new ThreadLocal<FSTConfiguration>() {
    @Override
    protected FSTConfiguration initialValue() {
        return configurations[((int) (Thread.currentThread().getId() % configurations.length))];
    }
};
static {
    for (int i = 0; i < configurations.length; i++) {
        configurations[i] = FSTConfiguration.createDefaultConfiguration();
    }
}

public static void main(String [ ] args) {

    conf.get().registerClass(TestClass.class);
    conf.get().setShareReferences(false);

    ExecutorService executor = Executors.newFixedThreadPool(4);

    try {
        for (int i = 0; i < 10; i++) {
            Runnable worker = new RunnableTestClass(i, conf);
            executor.execute(worker);
        }
        executor.shutdown();
    } catch (Exception e) {
        System.out.println(e);
    }
}

Thank you for yout help.

1

There are 1 answers

0
WoLFi On BEST ANSWER

The fault was, that I'm trying to serializise the ObjectOutput in my Runnable Class. I fixed this in this way:

public class RunnableTestClass implements Serializable, Runnable {

private FSTObjectOutput out;
private ObjectOutputStream stream;
private TestClass object;

public RunnableTestClass(long i, ThreadLocal<FSTConfiguration> conf) throws Exception {
    object = new TestClass(i);
    stream = new ObjectOutputStream(new FileOutputStream("out/FST.ser_parallel"));
    out = conf.get().getObjectOutput(stream);
}

@Override
public void run() {
    try {
        out.writeObject(object, TestClass.class);
        out.flush();
        stream.close();
    } catch (IOException e) {
        System.out.println(e);
    }
}

}

So I serializise only the "object" Object.