#vulnerable code 
                                public void registerInstance(String namespaceId, String serviceName, String clusterName, Instance instance) throws Exception {

        Service service = getService(namespaceId, serviceName);
        boolean serviceUpdated = false;
        if (service == null) {
            service = new Service();
            service.setName(serviceName);
            service.setNamespaceId(namespaceId);
            // now validate the service. if failed, exception will be thrown
            service.setLastModifiedMillis(System.currentTimeMillis());
            service.recalculateChecksum();
            service.valid();
            serviceUpdated = true;
        }

        if (!service.getClusterMap().containsKey(instance.getClusterName())) {

            Cluster cluster = new Cluster();
            cluster.setName(instance.getClusterName());
            cluster.setDom(service);
            cluster.init();

            if (service.getClusterMap().containsKey(cluster.getName())) {
                service.getClusterMap().get(cluster.getName()).update(cluster);
            } else {
                service.getClusterMap().put(cluster.getName(), cluster);
            }

            service.setLastModifiedMillis(System.currentTimeMillis());
            service.recalculateChecksum();
            service.valid();
            serviceUpdated = true;
        }

        if (serviceUpdated) {
            Lock lock = addLockIfAbsent(UtilsAndCommons.assembleFullServiceName(namespaceId, serviceName));
            Condition condition = addCondtion(UtilsAndCommons.assembleFullServiceName(namespaceId, serviceName));

            final Service finalService = service;
            GlobalExecutor.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        addOrReplaceService(finalService);
                    } catch (Exception e) {
                        Loggers.SRV_LOG.error("register or update service failed, service: {}", finalService, e);
                    }
                }
            });

            try {
                lock.lock();
                condition.await(5000, TimeUnit.MILLISECONDS);
            } finally {
                lock.unlock();
            }
        }

        if (service.allIPs().contains(instance)) {
            throw new NacosException(NacosException.INVALID_PARAM, "instance already exist: " + instance);
        }

        addInstance(namespaceId, serviceName, clusterName, instance.isEphemeral(), instance);
    } 
                            #location 24 
                            #vulnerability type THREAD_SAFETY_VIOLATION
#fixed code
                                public void registerInstance(String namespaceId, String serviceName, String clusterName, Instance instance) throws Exception {

        Service service = getService(namespaceId, serviceName);
        boolean serviceUpdated = false;
        if (service == null) {
            service = new Service();
            service.setName(serviceName);
            service.setNamespaceId(namespaceId);
            // now validate the service. if failed, exception will be thrown
            service.setLastModifiedMillis(System.currentTimeMillis());
            service.recalculateChecksum();
            service.valid();
            serviceUpdated = true;
        }

        if (!service.getClusterMap().containsKey(instance.getClusterName())) {

            Cluster cluster = new Cluster();
            cluster.setName(instance.getClusterName());
            cluster.setDom(service);
            cluster.init();

            if (service.getClusterMap().containsKey(cluster.getName())) {
                service.getClusterMap().get(cluster.getName()).update(cluster);
            } else {
                service.getClusterMap().put(cluster.getName(), cluster);
            }

            service.setLastModifiedMillis(System.currentTimeMillis());
            service.recalculateChecksum();
            service.valid();
            serviceUpdated = true;
        }

        // only local memory is updated:
        if (serviceUpdated) {
            putService(service);
        }

        if (service.allIPs().contains(instance)) {
            throw new NacosException(NacosException.INVALID_PARAM, "instance already exist: " + instance);
        }

        addInstance(namespaceId, serviceName, clusterName, instance.isEphemeral(), instance);
    }