【问题标题】:parallelize fast-serialisation java并行化快速序列化java
【发布时间】:2018-09-29 14:22:21
【问题描述】:

这是我第一次使用 Java 中的线程。 我试图并行化快速序列化。 但我收到以下错误:

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

那么如何正确地并行化快速序列化呢? 我的错在哪里?

这是我的可运行类:

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

这里是我的主要内容:

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

感谢您的帮助。

【问题讨论】:

  • FSTObjectOutput 类应该实现可序列化。
  • 我该如何实现?
  • 您正在尝试在某处序列化序列化器er,而不是您真正想要序列化的东西。
  • 谢谢,我发现了我的错误。

标签: java serialization parallel-processing fst


【解决方案1】:

错误是,我试图序列化我的 Runnable 类中的 ObjectOutput。 我以这种方式解决了这个问题:

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

}

所以我只序列化“对象”对象。

【讨论】:

    猜你喜欢
    • 2011-10-25
    • 2014-08-29
    • 1970-01-01
    • 2021-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多