gpt4 book ai didi

java - 并行化快速序列化java

转载 作者:太空宇宙 更新时间:2023-11-04 10:09:00 24 4
gpt4 key购买 nike

这是我第一次在 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);
}
}

感谢您的帮助。

最佳答案

错误是,我正在尝试序列化我的 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);
}
}

}

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

关于java - 并行化快速序列化java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52569271/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com