You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2021/05/27 15:59:37 UTC

[shardingsphere] branch master updated: Use daemon threads in SchemaBuilder (#10510)

This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new bce69c7  Use daemon threads in SchemaBuilder (#10510)
bce69c7 is described below

commit bce69c779e8063ac097429c0ba10c181eee6d524
Author: 吴伟杰 <wu...@apache.org>
AuthorDate: Thu May 27 23:55:37 2021 +0800

    Use daemon threads in SchemaBuilder (#10510)
---
 .../metadata/schema/builder/SchemaBuilder.java     | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SchemaBuilder.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SchemaBuilder.java
index a83a19e..bda0f60 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SchemaBuilder.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/SchemaBuilder.java
@@ -45,8 +45,12 @@ import java.util.Map.Entry;
 import java.util.Optional;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
 
 /**
@@ -55,7 +59,8 @@ import java.util.stream.Collectors;
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
 public final class SchemaBuilder {
     
-    private static final ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
+    private static final ExecutorService EXECUTOR_SERVICE = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors() * 2, Runtime.getRuntime().availableProcessors() * 2,
+            0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), new SchemaBuilderThreadFactory());
     
     static {
         ShardingSphereServiceLoader.register(DialectTableMetaDataLoader.class);
@@ -121,7 +126,6 @@ public final class SchemaBuilder {
                 throw new ShardingSphereException(ex);
             }
         }
-        EXECUTOR_SERVICE.shutdown();
     }
     
     private static void appendDefaultRemainTables(final SchemaBuilderMaterials materials, final ShardingSphereSchema schema) throws SQLException {
@@ -157,4 +161,16 @@ public final class SchemaBuilder {
         result.addAll(schema.getAllTableNames());
         return result;
     }
+    
+    private static class SchemaBuilderThreadFactory implements ThreadFactory {
+        
+        private final AtomicInteger threadSequence = new AtomicInteger(0);
+    
+        @Override
+        public Thread newThread(final Runnable runnable) {
+            Thread result = new Thread(runnable, String.format("SchemaBuilderExecutor-%d", threadSequence.getAndIncrement()));
+            result.setDaemon(true);
+            return result;
+        }
+    }
 }