Skip to content

Commit

Permalink
make all ThreadPool static final
Browse files Browse the repository at this point in the history
  • Loading branch information
robberphex committed Oct 19, 2023
1 parent d00798f commit 2906633
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@
import com.alibaba.csp.sentinel.concurrent.NamedThreadFactory;

import java.io.UnsupportedEncodingException;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.*;
import java.util.concurrent.*;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.StreamHandler;

/**
* This Handler publishes log records to console by using {@link java.util.logging.StreamHandler}.
Expand All @@ -41,6 +38,19 @@
* @author cdfive
*/
class ConsoleHandler extends Handler {
private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(
1,
1,
1,
TimeUnit.HOURS,
new ArrayBlockingQueue<>(1024),
new NamedThreadFactory("sentinel-console-log-executor", true),
new LogRejectedExecutionHandler()
);

static {
executor.allowCoreThreadTimeOut(true);
}

/**
* A Handler which publishes log records to System.out.
Expand All @@ -52,21 +62,9 @@ class ConsoleHandler extends Handler {
*/
private StreamHandler stderrHandler;

private ExecutorService executor;

public ConsoleHandler() {
this.stdoutHandler = new StreamHandler(System.out, new CspFormatter());
this.stderrHandler = new StreamHandler(System.err, new CspFormatter());

int corePoolSize = 1;
int maximumPoolSize = 1;
long keepAliveTime = 0;
/**insure the log can be recorded*/
int queueSize = 1024;
RejectedExecutionHandler handler = new LogRejectedExecutionHandler();
executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize,
keepAliveTime, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(queueSize),
new NamedThreadFactory("sentinel-console-log-executor", true), handler);
}

@Override
Expand Down Expand Up @@ -94,8 +92,6 @@ public void flush() {

@Override
public void close() throws SecurityException {
/**not need to record log if process is killed.*/
executor.shutdown();
stdoutHandler.close();
stderrHandler.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayDeque;
import java.util.Calendar;
import java.util.Date;
import java.util.Queue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
Expand All @@ -43,7 +40,20 @@ public SimpleDateFormat initialValue() {
}
};

private ExecutorService executor;
private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(
1,
1,
1,
TimeUnit.HOURS,
new ArrayBlockingQueue<Runnable>(1024),
new NamedThreadFactory("sentinel-datafile-log-executor", true),
new ThreadPoolExecutor.DiscardOldestPolicy()
);

static {
// allow all thread could be stopped
executor.allowCoreThreadTimeOut(true);
}

private volatile FileHandler handler;

Expand All @@ -66,22 +76,10 @@ public SimpleDateFormat initialValue() {
this.append = append;
rotateDate();
this.initialized = true;

int corePoolSize = 1;
int maximumPoolSize = 1;
long keepAliveTime = 0;
/**insure the log can be recorded*/
int queueSize = 1024;
RejectedExecutionHandler handler = new LogRejectedExecutionHandler();
executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize,
keepAliveTime, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(queueSize),
new NamedThreadFactory("sentinel-datafile-log-executor", true), handler);
}

@Override
public void close() throws SecurityException {
/**not need to record log if process is killed.*/
executor.shutdown();
handler.close();
}

Expand Down

0 comments on commit 2906633

Please sign in to comment.