diff --git a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistry.java b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistry.java index 3c98e1c3e358..52c9f9668200 100644 --- a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistry.java +++ b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistry.java @@ -473,26 +473,30 @@ private boolean isAdminProtocol(URL url) { private void scheduleServiceNamesLookup(final URL url, final NacosAggregateListener listener) { if (scheduledExecutorService == null) { - scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); - scheduledExecutorService.scheduleAtFixedRate( - () -> { - Set serviceNames = getAllServiceNames(); - filterData(serviceNames, serviceName -> { - boolean accepted = false; - for (String category : ALL_SUPPORTED_CATEGORIES) { - String prefix = category + SERVICE_NAME_SEPARATOR; - if (serviceName != null && serviceName.startsWith(prefix)) { - accepted = true; - break; - } - } - return accepted; - }); - doSubscribe(url, listener, serviceNames); - }, - LOOKUP_INTERVAL, - LOOKUP_INTERVAL, - TimeUnit.SECONDS); + synchronized (this) { + if (scheduledExecutorService == null) { + scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); + scheduledExecutorService.scheduleAtFixedRate( + () -> { + Set serviceNames = getAllServiceNames(); + filterData(serviceNames, serviceName -> { + boolean accepted = false; + for (String category : ALL_SUPPORTED_CATEGORIES) { + String prefix = category + SERVICE_NAME_SEPARATOR; + if (serviceName != null && serviceName.startsWith(prefix)) { + accepted = true; + break; + } + } + return accepted; + }); + doSubscribe(url, listener, serviceNames); + }, + LOOKUP_INTERVAL, + LOOKUP_INTERVAL, + TimeUnit.SECONDS); + } + } } }