Skip to content

Commit

Permalink
must shutdown thread pool when no in use (apache#3255)
Browse files Browse the repository at this point in the history
* must shutdown thread pool when no in use
  • Loading branch information
scxwhite authored and khanimteyaz committed Jan 18, 2019
1 parent 3ba5c8c commit 8893944
Showing 1 changed file with 27 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.ExecutorService;

/**
* Abort Policy.
Expand Down Expand Up @@ -78,43 +78,34 @@ private void dumpJStack() {
return;
}

Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
String dumpPath = url.getParameter(Constants.DUMP_DIRECTORY, System.getProperty("user.home"));

SimpleDateFormat sdf;

String os = System.getProperty("os.name").toLowerCase();

// window system don't support ":" in file name
if(os.contains("win")){
sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
}else {
sdf = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
}

String dateStr = sdf.format(new Date());
FileOutputStream jstackStream = null;
try {
jstackStream = new FileOutputStream(new File(dumpPath, "Dubbo_JStack.log" + "." + dateStr));
JVMUtil.jstack(jstackStream);
} catch (Throwable t) {
logger.error("dump jstack error", t);
} finally {
guard.release();
if (jstackStream != null) {
try {
jstackStream.flush();
jstackStream.close();
} catch (IOException e) {
}
}
}

lastPrintTime = System.currentTimeMillis();
ExecutorService pool = Executors.newSingleThreadExecutor();
pool.execute(() -> {
String dumpPath = url.getParameter(Constants.DUMP_DIRECTORY, System.getProperty("user.home"));

SimpleDateFormat sdf;

String os = System.getProperty("os.name").toLowerCase();

// window system don't support ":" in file name
if (os.contains("win")) {
sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
} else {
sdf = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
}

String dateStr = sdf.format(new Date());
//try-with-resources
try (FileOutputStream jStackStream = new FileOutputStream(new File(dumpPath, "Dubbo_JStack.log" + "." + dateStr))) {
JVMUtil.jstack(jStackStream);
} catch (Throwable t) {
logger.error("dump jStack error", t);
} finally {
guard.release();
}
lastPrintTime = System.currentTimeMillis();
});
//must shutdown thread pool ,if not will lead to OOM
pool.shutdown();

}

Expand Down

0 comments on commit 8893944

Please sign in to comment.