Java非算法手撕

线程池

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

public class Solution {
public static void main(String[] args) {
int corePoolSize = 2;
int maximumPoolSize = 4;
long keepAliveTime = 10L;

ThreadPoolExecutor pool = new ThreadPoolExecutor(
corePoolSize,
maximumPoolSize,
keepAliveTime,
TimeUnit.SECONDS,
new LinkedBlockingDeque<>(),
new CustomThreadFactory(),
new ThreadPoolExecutor.AbortPolicy()
);

// 提交任务到线程池
for (int i = 0; i < 10; i++) {
pool.execute(new Task(i));
}

// 关闭线程池
pool.shutdown();
try {
if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
pool.shutdownNow();
}
} catch (InterruptedException ex) {
pool.shutdownNow();
Thread.currentThread().interrupt();
}
}
}

class CustomThreadFactory implements ThreadFactory {
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix = "pool-";

@Override
public Thread newThread(Runnable r) {
return new Thread(r, namePrefix + threadNumber.getAndIncrement());
}
}

class Task implements Runnable {
private final int taskId;

public Task(int taskId) {
this.taskId = taskId;
}

@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " is executing task " + taskId);
try {
Thread.sleep(2000); // 模拟任务执行时间
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}

JDK动态代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public interface UserService {  // 接口(必须基于接口)
void addUser(String name);
String getUser(int id);
}
public class UserServiceImpl implements UserService { // 真实对象(目标类)
@Override
public void addUser(String name) {
System.out.println("添加用户: " + name);
}
@Override
public String getUser(int id) {
return "用户" + id;
}
}
public class UserServiceProxyHandler implements InvocationHandler {
private final Object target; // 真实对象
public UserServiceProxyHandler(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// 前置增强
System.out.println("调用方法前: " + method.getName());
// 调用真实对象的方法
Object result = method.invoke(target, args);
// 后置增强
System.out.println("调用方法后: " + method.getName());
return result;
}
}
public class Main {
public static void main(String[] args) {
// 真实对象
UserService realService = new UserServiceImpl();
// 创建代理对象
UserService proxy = (UserService) Proxy.newProxyInstance(
realService.getClass().getClassLoader(), // 类加载器
realService.getClass().getInterfaces(), // 接口列表
new UserServiceProxyHandler(realService) // InvocationHandler
);
// 通过代理对象调用方法
proxy.addUser("Alice"); // 触发增强逻辑
System.out.println(proxy.getUser(1));
}
}