I encounter NullPointerExceptions when I use Java-Serialization in
multiple threads (.NET v1.1.4322, vjslib 1.0.5000.0). Here my test
program:
package SerializationCrash;
import java.io.*;
public class Program
{
public static class Transfer implements java.io.Serializable
{
public int _a;
public String _b;
public double _c;
public Transfer()
{}
public Transfer(int a, String b, double c)
{
_a = a;
_b = b;
_c = c;
}
}
public static class Runner implements Runnable
{
private int _no;
public Runner(int no)
{
_no = no;
}
public void run()
{
try
{
Transfer[] t = new Transfer[100];
for(int j = 0; j < t.length; j++)
t[j] = new Transfer(j, "Transfer " + j, j + 0.5);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(t);
oos.flush();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
System.out.println("Thread " + _no + " finished");
}
}
}
/** @attribute System.STAThread() */
public static void main(String[] args) throws Exception, RuntimeException
{
Transfer tr = new Transfer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(tr);
Thread[] t = new Thread[100];
for(int j = 0; j < t.length; j++)
{
t[j] = new Thread(new Runner(j));
t[j].start();
}
for(int j = 0; j < t.length; j++)
{
if(t[j].isAlive())
t[j].join();
}
}
}
Most of the threads crash with the following stack trace:
java.lang.NullPointerException: Object reference not set to an instance of an object.
at java.io.ObjectOutputStream.defaultWriteObject()
at java.io.ObjectOutputStream.writeObject(Object obj)
at java.io.ObjectOutputStream.writeArrayElements(Object obj, Int32 len, Class elemType)
at java.io.ObjectOutputStream.writeObject(Object obj)
at SerializationCrash.Runner.run()
When I run this program with only a bunch of threads (say 5) then it
seems to work seamlessly. Another thing I recognized: when I serialize
a Transfer-Object once before I start the threads, the error doesn't
seem to happen:
public static void main(String[] args) throws Exception, RuntimeException
{
Transfer tr = new Transfer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(tr);
Thread[] t = new Thread[100];
....
Do you know anything about issues concerning using Java-Serialization
in multiple threads concurrently Perhaps the threads share
serialization information about the Transfer-Class which is not
synchronized.
Any hints would be welcome !
Best regards,
Michael

Crash in Serialization + Multiple Threads
S Collins
Best regards,
Mike
guru13
Your application is working perfectly with .NET 2.0 (Visual Studio 2005).
Thread 0 finished
Thread 1 finished
Thread 2 finished
Thread 3 finished
Thread 4 finished
Thread 5 finished
Thread 6 finished
Thread 7 finished
Thread 8 finished
Thread 9 finished
Thread 10 finished
Thread 11 finished
Thread 12 finished
Thread 13 finished
.
.
.
Thread 99 finished