我正在编写一个网络单例类,它需要在一个线程中运行,到目前为止没问题,但是我无法让它工作。
网络类:
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();
}
}
控制器类的一部分:
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() 发送到固定服务器。该服务器当然也可以发回东西,但是在那个时候,网络需要在某个时候做出反应并从控制器调用方法。
问候。
编辑:建议的解决方案,基于反应
网络类:
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();
}
这只是为了测试,实际上连接需要保持打开状态,直到用户关闭程序。