This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch 3.2
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.2 by this push:
new ae98de212b Perfect some code (#11533)
ae98de212b is described below
commit ae98de212be458a5f1f011be5ebdd47da8d9a7e8
Author: KamTo Hung <[email protected]>
AuthorDate: Wed Feb 15 06:54:46 2023 +0800
Perfect some code (#11533)
* Perfect some code
1. Replace filter...findAny…isPresent to anyMatch, we can reduce some
circulation
2. Reduce some new ArrayList<>()
3. Add 'final' for member variable
* Perfect some code. (#11534)
* Perfect some code. (#11534)
* Perfect some code. (#11534)
* Perfect some code. (#11534)
---
.../apache/dubbo/rpc/model/ApplicationModel.java | 8 +++---
.../dubbo/rpc/model/ApplicationModelTest.java | 31 +++++++++++++++++++++-
.../config/deploy/DefaultApplicationDeployer.java | 10 +++----
.../dubbo/config/deploy/DefaultModuleDeployer.java | 18 +++++--------
4 files changed, 46 insertions(+), 21 deletions(-)
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ApplicationModel.java
b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ApplicationModel.java
index a2cd0b84d9..1a10209931 100644
---
a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ApplicationModel.java
+++
b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ApplicationModel.java
@@ -28,7 +28,6 @@ import org.apache.dubbo.common.utils.Assert;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.context.ConfigManager;
-import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
@@ -68,8 +67,8 @@ public class ApplicationModel extends ScopeModel {
private volatile ModuleModel defaultModule;
// internal module index is 0, default module index is 1
- private AtomicInteger moduleIndex = new AtomicInteger(0);
- private Object moduleLock = new Object();
+ private final AtomicInteger moduleIndex = new AtomicInteger(0);
+ private final Object moduleLock = new Object();
// --------- static methods ----------//
@@ -251,7 +250,7 @@ public class ApplicationModel extends ScopeModel {
frameworkModel.tryDestroyProtocols();
// 4. destroy application resources
- for (ModuleModel moduleModel : new ArrayList<>(moduleModels)) {
+ for (ModuleModel moduleModel : moduleModels) {
if (moduleModel != internalModule) {
moduleModel.destroy();
}
@@ -456,4 +455,5 @@ public class ApplicationModel extends ScopeModel {
public void setDeployer(ApplicationDeployer deployer) {
this.deployer = deployer;
}
+
}
diff --git
a/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/ApplicationModelTest.java
b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/ApplicationModelTest.java
index 318ab343c3..782b7b6004 100644
---
a/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/ApplicationModelTest.java
+++
b/dubbo-common/src/test/java/org/apache/dubbo/rpc/model/ApplicationModelTest.java
@@ -27,6 +27,11 @@ import
org.apache.dubbo.rpc.support.MockScopeModelDestroyListener;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CountDownLatch;
+
/**
* {@link ApplicationModel}
*/
@@ -144,4 +149,28 @@ class ApplicationModelTest {
}
-}
\ No newline at end of file
+ @Test
+ void testCopyOnWriteArrayListIteratorAndRemove() throws
InterruptedException {
+ List<Integer> cur = new ArrayList<>();
+ for (int i = 0; i < 10000; i++) {
+ cur.add(i);
+ }
+ List<Integer> myList = new CopyOnWriteArrayList<>(cur);
+ List<Thread> threads = new ArrayList<>();
+ int threadNum = 20;
+ CountDownLatch endLatch = new CountDownLatch(threadNum);
+ for (int i = 0; i < 20; i++) {
+ threads.add(new Thread(() -> {
+ for (Integer number : myList) {
+ if (number % 2 == 0) {
+ myList.remove(number);
+ }
+ }
+ endLatch.countDown();
+ }));
+ }
+ threads.forEach(Thread::start);
+ endLatch.await();
+ }
+
+}
diff --git
a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployer.java
b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployer.java
index f1174c5759..5d82ac7da2 100644
---
a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployer.java
+++
b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployer.java
@@ -229,9 +229,8 @@ public class DefaultApplicationDeployer extends
AbstractDeployer<ApplicationMode
private void initModuleDeployers() {
// make sure created default module
applicationModel.getDefaultModule();
- // copy modules and initialize avoid ConcurrentModificationException
if add new module
- List<ModuleModel> moduleModels = new
ArrayList<>(applicationModel.getModuleModels());
- for (ModuleModel moduleModel : moduleModels) {
+ // deployer initialize
+ for (ModuleModel moduleModel : applicationModel.getModuleModels()) {
moduleModel.getDeployer().initialize();
}
}
@@ -513,7 +512,7 @@ public class DefaultApplicationDeployer extends
AbstractDeployer<ApplicationMode
*/
private boolean supportsExtension(Class<?> extensionClass, String name) {
if (isNotEmpty(name)) {
- ExtensionLoader extensionLoader =
getExtensionLoader(extensionClass);
+ ExtensionLoader<?> extensionLoader =
getExtensionLoader(extensionClass);
return extensionLoader.hasExtension(name);
}
return false;
@@ -673,7 +672,7 @@ public class DefaultApplicationDeployer extends
AbstractDeployer<ApplicationMode
prepareInternalModule();
// filter and start pending modules, ignore new module during
starting, throw exception of module start
- for (ModuleModel moduleModel : new
ArrayList<>(applicationModel.getModuleModels())) {
+ for (ModuleModel moduleModel : applicationModel.getModuleModels()) {
if (moduleModel.getDeployer().isPending()) {
moduleModel.getDeployer().start();
}
@@ -1147,4 +1146,5 @@ public class DefaultApplicationDeployer extends
AbstractDeployer<ApplicationMode
return configManager.getApplicationOrElseThrow();
}
+
}
diff --git
a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultModuleDeployer.java
b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultModuleDeployer.java
index 2965fcb455..25b6363914 100644
---
a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultModuleDeployer.java
+++
b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultModuleDeployer.java
@@ -64,18 +64,18 @@ public class DefaultModuleDeployer extends
AbstractDeployer<ModuleModel> impleme
private final List<CompletableFuture<?>> asyncReferringFutures = new
ArrayList<>();
- private List<ServiceConfigBase<?>> exportedServices = new ArrayList<>();
+ private final List<ServiceConfigBase<?>> exportedServices = new
ArrayList<>();
- private ModuleModel moduleModel;
+ private final ModuleModel moduleModel;
- private FrameworkExecutorRepository frameworkExecutorRepository;
- private ExecutorRepository executorRepository;
+ private final FrameworkExecutorRepository frameworkExecutorRepository;
+ private final ExecutorRepository executorRepository;
private final ModuleConfigManager configManager;
private final SimpleReferenceCache referenceCache;
- private ApplicationDeployer applicationDeployer;
+ private final ApplicationDeployer applicationDeployer;
private CompletableFuture startFuture;
private Boolean background;
private Boolean exportAsync;
@@ -452,18 +452,14 @@ public class DefaultModuleDeployer extends
AbstractDeployer<ModuleModel> impleme
return moduleModel.getConfigManager().getProviders()
.stream()
.map(ProviderConfig::getExportBackground)
- .filter(k -> k != null && k)
- .findAny()
- .isPresent();
+ .anyMatch(k -> k != null && k);
}
private boolean isReferBackground() {
return moduleModel.getConfigManager().getConsumers()
.stream()
.map(ConsumerConfig::getReferBackground)
- .filter(k -> k != null && k)
- .findAny()
- .isPresent();
+ .anyMatch(k -> k != null && k);
}
@Override