似乎不可能使用Java时,可能未初始化编译器警告映射 [英] Compiler warning map may not of been initialised when it seems impossible Java

查看:69
本文介绍了似乎不可能使用Java时,可能未初始化编译器警告映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的这个线程有3个try catch.第一种是尝试使用资源来设置ObjectOutputStream.第二个从另一个接收有关认证是成功还是失败的信息.成功时,它应建立一个用于通信的映射,失败时,应从中返回线程.同样,如果在此阶段发生Interrupt或IOException,则从中返回线程.据我所知,只有在成功通过身份验证的情况下,才可以尝试第二次尝试.第二个块负责处理它收到的数据包,直到会话通过中断或请求它的数据包结束.我的问题是,在结束会话时,我需要用空的Optional替换与该线程有关的上述ConcurrenHashMap记录.这应该在前面概述的两种关机机制中都发生.但是,在InterruptedException catch块中负责此操作的行说,尽管实际上没有初始化就不可能到达该块,但该地图可能尚未初始化.

This thread I have written has three try catches. The first is a try with resources to set up an ObjectOutputStream. The second recieves information from another on whether authentication has succeeded or failed. On success it should establish a map utilised in communication and on failure the thread is returned from. Likewise if an Interrupt or IOException occurs in this phase, the thread is returned from. Only in the eventuality of successful authentication as far as I can see should the second try catch be reached. This second block is responsible for handling packets it receives util the session ends either through interrupt or a packet requesting it. My problem is that on ending a session I am required to replace the aforementioned ConcurrenHashMap record pertaining to this thread with an empty Optional. this should occur in both previously outlined shutdown mechanisms. However the line responsible for this in the InterruptedException catch block says the map may not have been initialised despite the fact that it should be impossible to reach that block without its initialisation.

public void run(){

        boolean quit = false;
        Packet packet;
        int cid, nid;
        ConcurrentMap<Integer, Optional<BlockingQueue<Packet>>> channelMap;

        try (ObjectOutputStream output = new ObjectOutputStream(new 
BufferedOutputStream(sslSocket.getOutputStream()))) {
            try {
            packet = channel.take();
            if (packet.getType() == AUTH_SUCCESS) {
                cid = ((AuthSuccessPacket) packet).getCid();
                nid = ((AuthSuccessPacket) packet).getNid();
                channelMap = networkMap.get(nid);
                channelMap.replace(cid, Optional.of(channel));
                output.writeObject(packet);
            } else {
                output.writeObject(packet);
                return;
            }
        }catch (IOException | InterruptedException e){
            return;
        }

            while (!quit && !interrupted()) {
                try {
                    packet = channel.take();
                switch (packet.getType()) {
                    case ACK:
                    case MESSAGE:
                    case REQUEST_USER:
                    case RELAY_SHUTDOWN:
                        output.writeObject(packet);
                        break;
                    case END_SESSION:
                        if (packet.getSource() == cid) {
                            output.writeObject(packet);
                            channelMap.replace(cid, Optional.empty());
                            quit = true;
                        }
                        break;
                }
                }catch (IOException e){}
            }
    }catch (InterruptedException e){
        channelMap.replace(cid, Optional.empty());
    } catch (IOException e){}
}

我想念什么?谢谢.

推荐答案

我不确定是什么原因,但是我将Interrupt移到了第二个内部块的单独捕获中,并且不再引发异常.

I am not sure what the cause was but I moved the Interrupt to a separate catch of the second inner block and it no longer raised the exception.

这篇关于似乎不可能使用Java时,可能未初始化编译器警告映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆