我有一个ObjectOutputStream和一个ObjectInputStream。我尝试通过它们发送int和对象。现在我设法发送和阅读到一个点,但我不知道为什么它会停在这里。
这就是要点:
读者:
while (true) {
start = in.readInt();
System.out.println("PART 1");
int temp1 = in.readInt();
int temp2 = in.readInt();
int temp3 = in.readInt();
System.out.println("PART12");
Chunk temp = new Chunk(temp1,temp2, temp3);
while (true) {它不会到达part12 (不会传递第一个int...)
作者:
if (chunkList != null) {
for (Chunk c: chunkList) {
out.writeInt(-1);
out.writeInt(c.getLocation().getX());
out.writeInt(c.getLocation().getY());
out.writeInt(c.getLocation().getZ());
if (c.getTileList() != null) {它成功地通过了所有这些操作。
我在一个单独的线程中每隔2ms运行一次out.flushing。
线程:
while (true)
{
while (c.sendPacket()) {
try
{
if (c.getOut() != null)
{
c.getOut().flush();
}
}
catch (IOException ioexception)
{
ioexception.printStackTrace();
}
try
{
sleep(2L);
}
catch (InterruptedException interruptedexception) { }
}
}为什么它在包含3个整数的部分停止读取?
发布于 2012-05-26 09:54:35
我感觉这是一个线程安全问题。一般来说,流并不是设计成线程安全的。因此,除非你在更高的级别上同步这两个线程,否则一个线程写入流而另一个线程调用flush是不安全的。
https://stackoverflow.com/questions/10762196
复制相似问题