나는 당신이 원하는 것을 완전히 확신하지 못합니다. 한 번에 하나의 스레드 만 하나의 특정 서버에 액세스하는 것을 보장하려고한다고 가정합니다. 연결 소켓과 같이 인 경우
, 당신은 synchronization statement에 자물쇠로 사용할 수 있습니다 :
private void send(Connection c, Data d) {
synchronized (c) {
// for each connection object, only one thread may be inside this block.
// all other threads wait until the thread currently in this block exits it.
c.send(d);
}
}
// somewhere else ...
Data data = determineDataToSend()
Connection connection = map.get(key);
send(connection, data)
당신은 연결에 대한 장식로도 논리를 넣을 수 있습니다.
public interface PowerfulConnection {
public void doA();
public int doB(ParameterForB param);
}
public class ConnectionImpl implements PowerfulConnection {
// handles the actual connection
}
/**
* This method is a decorator for PowerfulConnection that synchronizes all method accesses.
*/
public class SynchronizedConnection implements PowerfulConnection {
private PowerfulConnection target;
public SynchronizedConnection(PowerfulConnection target) {
if (target == null) throw new NullPointerException();
this.target = target;
}
public synchronized void doA() {
target.doA();
}
public synchronized int doB(ParameterForB param) {
return target.doB(param);
}
}
당신이 장식 접근 방식을 사용하는 경우, 유일한 것은 당신이 (당신은 RMI와 같은 높은 추상화 수준을 사용하기 때문에, 예를 들어)이 연결이 보내거나받을 하나 개 이상의 방법이있는 경우 특히 유용합니다 변경해야하는 것은 인스턴스 작성입니다. 대신에 :
private void connect(key, connectionParams) {
map.put(key, new ConnectionImpl(connectionParams));
}
사용
private void connect(key, connectionParams) {
map.put(key, new SynchronizedConnection(new ConnectionImpl(connectionParams)));
}
다중 서버 란 무엇입니까? – SLaks
내 프로그램이 많은 서버에 연결 – QuanNH
서버용 서버 소켓을 사용합니까? – SjB