-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(all): tune single Thread into SingleThreadExecutor (#5410)
- Loading branch information
1 parent
f33c169
commit a9c4f43
Showing
7 changed files
with
150 additions
and
40 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
common/src/main/java/org/tron/common/es/ExecutorServiceManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.tron.common.es; | ||
|
||
import com.google.common.util.concurrent.ThreadFactoryBuilder; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j(topic = "common") | ||
public class ExecutorServiceManager { | ||
|
||
public static ExecutorService newSingleThreadExecutor(String name) { | ||
return newSingleThreadExecutor(name, false); | ||
} | ||
|
||
public static ExecutorService newSingleThreadExecutor(String name, boolean isDaemon) { | ||
return Executors.newSingleThreadExecutor( | ||
new ThreadFactoryBuilder().setNameFormat(name).setDaemon(isDaemon).build()); | ||
} | ||
|
||
|
||
public static ScheduledExecutorService newSingleThreadScheduledExecutor(String name) { | ||
return newSingleThreadScheduledExecutor(name, false); | ||
} | ||
|
||
public static ScheduledExecutorService newSingleThreadScheduledExecutor(String name, | ||
boolean isDaemon) { | ||
return Executors.newSingleThreadScheduledExecutor( | ||
new ThreadFactoryBuilder().setNameFormat(name).setDaemon(isDaemon).build()); | ||
} | ||
|
||
public static void shutdownAndAwaitTermination(ExecutorService pool, String name) { | ||
if (pool == null) { | ||
return; | ||
} | ||
logger.info("Pool {} shutdown...", name); | ||
pool.shutdown(); // Disable new tasks from being submitted | ||
try { | ||
// Wait a while for existing tasks to terminate | ||
if (!pool.awaitTermination(60, java.util.concurrent.TimeUnit.SECONDS)) { | ||
pool.shutdownNow(); // Cancel currently executing tasks | ||
// Wait a while for tasks to respond to being cancelled | ||
if (!pool.awaitTermination(60, java.util.concurrent.TimeUnit.SECONDS)) { | ||
logger.warn("Pool {} did not terminate", name); | ||
} | ||
} | ||
} catch (InterruptedException ie) { | ||
// (Re-)Cancel if current thread also interrupted | ||
pool.shutdownNow(); | ||
// Preserve interrupt status | ||
Thread.currentThread().interrupt(); | ||
} | ||
logger.info("Pool {} shutdown done", name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
framework/src/test/java/org/tron/common/backup/BackupServerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.tron.common.backup; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
import org.tron.common.backup.socket.BackupServer; | ||
import org.tron.common.parameter.CommonParameter; | ||
import org.tron.core.Constant; | ||
import org.tron.core.config.args.Args; | ||
|
||
|
||
public class BackupServerTest { | ||
|
||
@Rule | ||
public TemporaryFolder temporaryFolder = new TemporaryFolder(); | ||
private BackupServer backupServer; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
Args.setParam(new String[]{"-d", temporaryFolder.newFolder().toString()}, Constant.TEST_CONF); | ||
CommonParameter.getInstance().setBackupPort(80); | ||
List<String> members = new ArrayList<>(); | ||
members.add("127.0.0.2"); | ||
CommonParameter.getInstance().setBackupMembers(members); | ||
BackupManager backupManager = new BackupManager(); | ||
backupManager.init(); | ||
backupServer = new BackupServer(backupManager); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
backupServer.close(); | ||
Args.clearParam(); | ||
} | ||
|
||
@Test | ||
public void test() throws InterruptedException { | ||
backupServer.initServer(); | ||
} | ||
} |