繁体   English   中英

使用同步方法锁定时释放BufferedReader.readLine()

[英]Releasing BufferedReader.readLine() while locked with synchronized method

目前我有一个类似以下代码:

public class CtrlServer {
    private ServerSocket ss;
    private Map<Integer, Socket> s;
    private Map<Integer, PrintWriter> out;
    private Map<Integer, BufferedReader> in;

    public CtrlServer(){
        ss = null;
        s = new HashMap<Integer, Socket>();
        out = new HashMap<Integer, PrintWriter>();
        in = new HashMap<Integer, BufferedReader>();
    }
    public CtrlServer(int port) throws Exception{
        this();
        if(!initialize(port))
            throw new Exception();
    }
    public synchronized boolean initialize(int port){
        try{close();}catch(Exception e){}
        try{
            ss = new ServerSocket(port);
        } catch(Exception e){
            return false;
        }
        return true;
    }
    public boolean getClient(int id, int timeout){
        close(id);
        try{
            ss.setSoTimeout(timeout);
            Socket socket = ss.accept();
            s.put(id, socket);
            in.put(id, new BufferedReader(new InputStreamReader(socket.getInputStream())));
            out.put(id, new PrintWriter(socket.getOutputStream(), true));
            return true;
        }catch (Exception e){
            return false;
        }finally{
            try{ss.setSoTimeout(0);}catch(SocketException e){}
        }
    }
    public synchronized String getResponse(int id, String command){
        try{
            send(id, command);
            String response =  in.get(id).readLine();
            return response;
        }catch(Exception e){
            close(id);
            return null;
        }
    }
    public synchronized void send(int id, String message){
        try{
            out.get(id).println(message);
        }catch(Exception e){}
    }
    public void broadcast(String message){
        for(Entry<Integer, PrintWriter> writer: out.entrySet())
            send(writer.getKey(), message);
    }
    public synchronized boolean isAlive(int id){
        try{
            if(getResponse(id, "alive").equals("OK"))
                return true;
            else
                return false;
        }catch(Exception e){
            close(id);
            return false;
        }
    }
    public synchronized void close(int id){
        try{in.remove(id);}catch(Exception e){}
        try{out.remove(id);}catch(Exception e){}
        try{s.remove(id).close();}catch(Exception e){}
    }
    public void close(){
        try{ss.close();}catch (Exception e){}
        for(Map.Entry<Integer, Socket> client : s.entrySet()){
            close(client.getKey());
        }
    }
}

当要调用close()方法并且在ReadLine()部分的getResponse()上已经有另一个线程时,问题就来了

是否有任何方法可以强制getResponse或bufferedReader.ReadLine()引发异常,或以某种方式使其停止,释放资源或其他东西?

我将创建一个处理程序实例,该实例保存您的套接字,输入,输出并为每个处理程序运行一个读取线程。

这将替换并简化此处的大多数共享代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM