Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化性能 #203

Merged
merged 2 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.core.executor.support.AbstractDynamicExecutorSupport;
import cn.hippo4j.core.proxy.RejectedProxyUtil;
import cn.hippo4j.core.toolkit.SystemClock;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
Expand Down Expand Up @@ -37,7 +38,7 @@ public class DynamicThreadPoolExecutor extends AbstractDynamicExecutorSupport {
@Getter
private final AtomicLong rejectCount = new AtomicLong();

private final ThreadLocal<Long> startTime = new ThreadLocal();
private final ThreadLocal<Long> startTime = new ThreadLocal<>();

public DynamicThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
Expand Down Expand Up @@ -77,7 +78,7 @@ protected void beforeExecute(Thread t, Runnable r) {
return;
}

this.startTime.set(System.currentTimeMillis());
this.startTime.set(SystemClock.now());
}

@Override
Expand All @@ -88,7 +89,7 @@ protected void afterExecute(Runnable r, Throwable t) {

try {
long startTime = this.startTime.get();
long endTime = System.currentTimeMillis();
long endTime = SystemClock.now();
long executeTime;
boolean executeTimeAlarm = (executeTime = (endTime - startTime)) > executeTimeOut;
if (executeTimeAlarm && ApplicationContextHolder.getInstance() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public PoolRunStateInfo getWebRunStateInfo() {
Method getActiveCount = ReflectionUtils.findMethod(fieldObject.getClass(), "getActiveCount");
ReflectionUtils.makeAccessible(getActiveCount);
int activeCount = (int) ReflectionUtils.invokeMethod(getActiveCount, fieldObject);
activeCount = (activeCount <= 0) ? 0 : activeCount;
activeCount = Math.max(activeCount, 0);
// 当前负载
String currentLoad = CalculateUtil.divide(activeCount, maximumPoolSize) + "";
// 峰值负载
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cn.hippo4j.core.toolkit;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

/**
* @author : wh
* @date : 2022/4/25 17:03
* @description:
*/
public class SystemClock {

private final int period;

private final AtomicLong now;

private static final String THREAD_NAME ="system.clock";

private static class InstanceHolder {
private static final SystemClock INSTANCE = new SystemClock(1);
}

private SystemClock(int period) {
this.period = period;
this.now = new AtomicLong(System.currentTimeMillis());
scheduleClockUpdating();
}

private static SystemClock instance() {
return InstanceHolder.INSTANCE;
}

private void scheduleClockUpdating() {
ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(1, r -> {
Thread thread = new Thread(r, THREAD_NAME);
thread.setDaemon(true);
return thread;
});
scheduler.scheduleAtFixedRate(() -> now.set(System.currentTimeMillis()), period, period, TimeUnit.MILLISECONDS);
}

private long currentTimeMillis() {
return now.get();
}

/**
* 用来替换原来的System.currentTimeMillis()
*/
public static long now() {
return instance().currentTimeMillis();
}
}