好的,这是我的问题。
我需要创建一个套接字程序,可以处理来自我的客户端应用程序的多个连接(让我们称之为apps1)。我使用线程处理这个(所以每个连接被抛入一个新线程)
问题是我可以接受来自所有开放连接的请求,但是当我想发送响应时,我必须通过最新的连接发送它。所以,如果我有3个连接(con1,con2,con3),我可以接受来自con1,con2和con3的请求,但我必须通过con3发送响应(假设con3是最新的连接)
我想过使用带有PrintWriter参数的单例。因此,每次有新连接时,他们都会调用单例并更新参数,当我想发送响应时,我会在发送之前先获得PrintWriter。
这是我的Singleton类:
public class Singleton {
private static final Singleton instance = new Singleton();
PrintWriter out;
public static Singleton getInstance() {
return instance;
}
public Singleton ()
{
if (instance != null) {
throw new IllegalStateException("Already instantiated");
}
}
public PrintWriter getPrintWriter ()
{
return this.out;
}
public void updatePrintWriter (PrintWriter out){
this.out = out;
}
}
这是我的主要计划:
public class SocketAccept{
private ServerSocket mainSocket;
private Socket clientSocket;
public SocketAccept (int portNumber) {
Singleton s = Singleton.getInstance();
do {
try {
mainSocket = new ServerSocket(portNumber);
clientSocket = mainSocket.accept();
s.updatePrintWriter(new PrintWriter(clientSocket.getOutputStream(), true));
ClientThread (clientSocket);
} catch (IOException ex) {
Logger.getLogger(TestClass.class.getName()).log(Level.SEVERE, null, ex);
}
}while (true);//need to change this into thread pool or connection pool
}
}
这是我处理套接字的线程:
public class ClientThread extends Thread {
private Socket cs;
Singleton s = Singleton.getInstance();
PrintWriter out;
private String read(Socket sc) {
String request = "";
//read request here
return request;
}
private String process(String request) {
String response = "";
//process request here
return response;
}
public ClientThread(Socket clientSocket) {
this.cs = clientSocket;
}
@Override
public void run() {
String requestMsg = "";
String responseMsg = "";
do {
requestMsg = read(cs);// read the message
if (requestMsg.equalsIgnoreCase("SHUTDOWN")) {
break;
}
responseMsg = process(requestMsg);
out = s.getPrintWriter();
out.write(responseMsg);
} while (true);
}
}
我做得对吗?或者单身人士无法做到这一点?
感谢您的帮助。
答案 0 :(得分:3)
不幸的是,这不是Singleton模式的线程安全实现。在这种情况下,我认为你不需要一个,AtomicReference可能会正常工作。试试这个:
public class SocketAccept{
private ServerSocket mainSocket;
private Socket clientSocket;
private final AtomicReference<PrintWriter> printWriterHolder = new AtomicReference(null);
public SocketAccept (int portNumber) {
Singleton s = Singleton.getInstance();
do {
try {
mainSocket = new ServerSocket(portNumber);
clientSocket = mainSocket.accept();
printWriterHolder.set(new PrintWriter(clientSocket.getOutputStream(), true));
Thread clientThread = new ClientThread (clientSocket, printWriterHolder);
clientThread.start();
} catch (IOException ex) {
Logger.getLogger(TestClass.class.getName()).log(Level.SEVERE, null, ex);
}
}while (true);//need to change this into thread pool or connection pool
}
}
...
public class ClientThread extends Thread
...
private final AtomicReference<PrintWriter> printWriterHolder;
public ClientThread(Socket clientSocket, AtomicReference<PrintWriter> holder) {
this.cs = clientSocket;
this.printWriterHolder = holder;
}
@Override
public void run() {
String requestMsg = "";
String responseMsg = "";
do {
requestMsg = read(cs);// read the message
if (requestMsg.equalsIgnoreCase("SHUTDOWN")) {
break;
}
responseMsg = process(requestMsg);
out = printWriterHolder.get();
out.write(responseMsg);
} while (true);
}
}
如果你确实想要使用Singleton模式,这里是来自SO的引用,其中问题具有用于创建Singleton的良好线程安全实现:Java Singleton Pattern
您还需要使用synchronized
,Lock
或原子操作(AtomicInteger
,AtomicReference
等来访问Singleton的状态线程安全。)必要时。