Skip to content

Commit fda71e7

Browse files
scxwhitecarryxyh
authored andcommitted
must shutdown thread pool when no in use (apache#3255)
* must shutdown thread pool when no in use
1 parent 16fbc93 commit fda71e7

File tree

1 file changed

+27
-36
lines changed

1 file changed

+27
-36
lines changed

dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReport.java

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424

2525
import java.io.File;
2626
import java.io.FileOutputStream;
27-
import java.io.IOException;
2827
import java.text.SimpleDateFormat;
2928
import java.util.Date;
3029
import java.util.concurrent.Executors;
3130
import java.util.concurrent.RejectedExecutionException;
3231
import java.util.concurrent.Semaphore;
3332
import java.util.concurrent.ThreadPoolExecutor;
33+
import java.util.concurrent.ExecutorService;
3434

3535
/**
3636
* Abort Policy.
@@ -78,43 +78,34 @@ private void dumpJStack() {
7878
return;
7979
}
8080

81-
Executors.newSingleThreadExecutor().execute(new Runnable() {
82-
@Override
83-
public void run() {
84-
String dumpPath = url.getParameter(Constants.DUMP_DIRECTORY, System.getProperty("user.home"));
85-
86-
SimpleDateFormat sdf;
87-
88-
String os = System.getProperty("os.name").toLowerCase();
89-
90-
// window system don't support ":" in file name
91-
if(os.contains("win")){
92-
sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
93-
}else {
94-
sdf = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
95-
}
96-
97-
String dateStr = sdf.format(new Date());
98-
FileOutputStream jstackStream = null;
99-
try {
100-
jstackStream = new FileOutputStream(new File(dumpPath, "Dubbo_JStack.log" + "." + dateStr));
101-
JVMUtil.jstack(jstackStream);
102-
} catch (Throwable t) {
103-
logger.error("dump jstack error", t);
104-
} finally {
105-
guard.release();
106-
if (jstackStream != null) {
107-
try {
108-
jstackStream.flush();
109-
jstackStream.close();
110-
} catch (IOException e) {
111-
}
112-
}
113-
}
114-
115-
lastPrintTime = System.currentTimeMillis();
81+
ExecutorService pool = Executors.newSingleThreadExecutor();
82+
pool.execute(() -> {
83+
String dumpPath = url.getParameter(Constants.DUMP_DIRECTORY, System.getProperty("user.home"));
84+
85+
SimpleDateFormat sdf;
86+
87+
String os = System.getProperty("os.name").toLowerCase();
88+
89+
// window system don't support ":" in file name
90+
if (os.contains("win")) {
91+
sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
92+
} else {
93+
sdf = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
94+
}
95+
96+
String dateStr = sdf.format(new Date());
97+
//try-with-resources
98+
try (FileOutputStream jStackStream = new FileOutputStream(new File(dumpPath, "Dubbo_JStack.log" + "." + dateStr))) {
99+
JVMUtil.jstack(jStackStream);
100+
} catch (Throwable t) {
101+
logger.error("dump jStack error", t);
102+
} finally {
103+
guard.release();
116104
}
105+
lastPrintTime = System.currentTimeMillis();
117106
});
107+
//must shutdown thread pool ,if not will lead to OOM
108+
pool.shutdown();
118109

119110
}
120111

0 commit comments

Comments
 (0)