首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ObjectInputStream读取失败

ObjectInputStream读取失败
EN

Stack Overflow用户
提问于 2012-05-26 06:52:46
回答 1查看 418关注 0票数 0

我有一个ObjectOutputStream和一个ObjectInputStream。我尝试通过它们发送int和对象。现在我设法发送和阅读到一个点,但我不知道为什么它会停在这里。

这就是要点:

读者:

代码语言:javascript
复制
    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...)

作者:

代码语言:javascript
复制
 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。

线程:

代码语言:javascript
复制
 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个整数的部分停止读取?

EN

回答 1

Stack Overflow用户

发布于 2012-05-26 09:54:35

我感觉这是一个线程安全问题。一般来说,流并不是设计成线程安全的。因此,除非你在更高的级别上同步这两个线程,否则一个线程写入流而另一个线程调用flush是不安全的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10762196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档