ConcurrentHashMap 이용!!!
@Configuration
public class ConcurrentConfig {
public ConcurrentHashMap<String, SocketChannel> globalQtsocketMap = new ConcurrentHashMap<>(); //이 필드도 객체이므로 new로 인스턴스를 띄워줘야 된다.
//ConcurrentHashMap 은 multiThreadSafe
}
다른 스레드에서
@RequiredArgsConstructor
@Service
public class JsonParseService {
private static final Logger logger = LoggerFactory.getLogger(JsonParseService.class);
private final ConcurrentConfig concurrentConfig;
---위에서 DI-----
SocketChannel schn = new SocketChannel();
원하는 소켓 객체를 넣어주고.
concurrentConfig.globalQtsocketMap.put("mySchn", schn);
또 다른 스레드에서 그 객체를
@RequiredArgsConstructor
@Service
public class JsonParseService {
private static final Logger logger = LoggerFactory.getLogger(JsonParseService.class);
private final ConcurrentConfig concurrentConfig;
//위에서 DI
SocketChannel channel = concurrentConfig.globalQtsocketMap.get("mySchn"); / 가져온다.