java singleton网络实现runnable

时间:2013-04-26 20:30:02

标签: java networking runnable

我正在编写一个网络单例类,它需要在一个线程中运行,到目前为止没问题,但是我无法让它工作得很糟糕。

网络课程:

public class Network extends Thread {    
    private static Network cachedInstance = new Network();

    private PrintWriter out;
    private BufferedReader in;

    private Network() {        
    }

    private void init() {
        try {
            Socket clientSocket = new Socket(Config.HOST_NAME, Config.HOST_PORT);
            out = new PrintWriter(clientSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

            String fromServer;
            while ((fromServer = in.readLine()) != null) {
                System.out.println("Server: " + fromServer);
            }

        } catch (IOException ex) {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static Network getInstance() {
        return cachedInstance;
    }

    public void send(final String string) {
        out.println(string);
    }

    @Override
    public void run() {
        init();
    }
}

Controller类的一部分:

public void clientTest() {
    int random = new Random().nextInt(1000);
    Network.getInstance().start();
    Network.getInstance().send(random + "");
}

我得到的错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at network.Network.send(Network.java:52)
at controller.Controller.clientTest(Controller.java:126)

所以看起来单个网络实例没有正确实例化,理论上这是不可能的。

我的第二个问题是,如果我可以避免使用它:

Network.getInstance().start();

换句话说,我希望确保只创建一个线程(网络类),并且在初始化类时它始终默认运行。目前的方式还不错,但我认为它会更好。

对于想知道我为什么使用这种方法的人:基本上我只想使用Network.send()发送到固定服务器。那个服务器当然可以发回东西,但是在那个网络上需要做出反应并在某个时候从Controller调用方法。

问候。

编辑:建议的解决方案,基于反应

Network.class:

public class Network implements Runnable {    
    private static final Network cachedInstance;
    static {
        Network tempInstance = null;
        try {
            tempInstance = new Network(Config.HOST_NAME, Config.HOST_PORT);
        } catch (IOException ex) {
            Logger.getLogger(Network.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            cachedInstance = tempInstance;
        }
    }

    private final Socket clientSocket;
    private final PrintWriter out;
    private final BufferedReader in;

    private Network(final String hostname, final int port) throws IOException {
        clientSocket = new Socket(hostname, port);
        out = new PrintWriter(clientSocket.getOutputStream(), true);        
        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    }

    public static Network getInstance() {
        return cachedInstance;
    }

    @Override
    public void run() {
        try {
            String fromServer;
            while ((fromServer = in.readLine()) != null) {
                System.out.println("Server: " + fromServer);
            }
        } catch (IOException ex) {
            Logger.getLogger(Network.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void send(final String string) {
        out.println(string);
    }

    public void close() {
        try {
            in.close();
            out.close();
            clientSocket.close();
        } catch (IOException ex) {
            Logger.getLogger(Network.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

致电代码:

public void clientTest() {
    int random = new Random().nextInt(1000);
    Network network = Network.getInstance();
    new Thread(network).start();
    network.send(random + "");
    network.close();
}

这仅用于测试,实际上连接需要保持打开,直到用户关闭程序。

2 个答案:

答案 0 :(得分:2)

当您在某个线程上调用start()时,在线程实际运行之前会经过一段时间。您在调用send()之前调用run(),因此在初始化out之前。不要这样做。等待来自正在运行的线程的消息,只有这样才能安全地调用send()。您可以在简单wait()上使用notify()Object来执行此操作。

至于避免让客户致电start() - 确保从start()的构造函数中调用Network

答案 1 :(得分:1)

解决问题的一个简单方法是在启动线程之前建立连接,这样就不必担心这种竞争条件了。

public class Network implements Runnable, Closeable {    
    private final Socket clientSocket;
    private final PrintWriter out;
    private final BufferedReader in;
    private volatile boolean closed = false;

    public Network(String hostname, int port) throws IOException {        
        clientSocket = new Socket(hostname, port);
        out = new PrintWriter(clientSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    }

    public void run() {
        try {
            for(String fromServer; (fromServer = in.readLine()) != null;) 
                System.out.println("Server: " + fromServer);
        } catch (IOException ex) {
            if (!closed)
                Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void send(String line) {
        out.println(line);
    }

    public void close() {
        closed = true;
        try { clientSocket.close(); } catch (IOException ignored) { }
    }
}

进行测试

@Test
public void testClient() {
    Network network = new Network(Config.HOSTNAME, Config.PORT)
    new Thread(network).start();

    int random = new Random().nextInt(1000);
    network.send(random + "");
    network.close();
}

注意:使用有状态单例类使得单元测试非常困难,因为您必须将单例重置为其初始状态,或者一个测试可能会影响另一个,并且您运行的顺序可能会有所不同。在上面的测试中,它是自包含的,没有其他测试受到伤害。 ;)