导读
空指针异常
NPE 或许是编程语言中最常见的问题,被 Null 的发明者托尼·霍尔(Tony Hoare)称之为十亿美元的错误。在 Java 中并没有内置的处理 Null 值的语法,但仍然存在一些相对优雅的方式能够帮助我们的规避 NPE。
-
使用 JSR-305/jetbrain 等注解
-
NotNull -
Nullable
-
用 Optional 处理链式调用
public class OptionalExample {
public static void main(String[] args) {
// 使用传统空值处理方式
User user = getUser();
String city = "DEFAULT";
if (user != null && user.isValid()) {
Address address = user.getAddress();
if (adress != null) {
city = adress.getCity();
}
}
System.out.println(city);
// 使用 Optional 的方式
Optional
optional = getUserOptional(); city = optional.filter(User::isValid)
.map(User::getAddress)
.map(Adress::getCity)
.orElse("DEFAULT")
System.out.println(city);
}
public static User getUser() {
return null;
}
public static Optional
getUserOptional() {return Optional.empty();
}
public static class User {
private Adress address;
private boolean valid;
}
public static class Address {
private String city;
}
}
-
用 Objects.equals(a,b) 代替 a.equals(b)
-
使用空对象模式
public class EmptyListExample {
public static void main(String[] args) {
ListString> listNullable = getListNullable();
if (listNullable != null) {
for (String s : listNullable) {
System.out.println(s);
}
}
ListString> listNotNull = getListNotNull();
for (String s : listNotNull) {
System.out.println(s);
}
}
public static ListString> getListNullable() {
return null;
}
public static ListString> getListNotNull() {
return Collections.emptyList();
}
}
空策略
public class NullStrategyExample {
private static final Map
strategyMap = new HashMap();
public static void handle(String strategy, String content) {
findStrategy(strategy).handle(content);
}
private static Strategy findStrategy(String strategyKey) {
return strategyMap.getOrDefault(strategyKey, new DoNothing());
}
public interface Strategy {
void handle(String s);
}
// 当找不到对应策略时, 什么也不做
public static class DoNothing implements Strategy {
public void handle(String s) {
}
}
}
对象转化
public class UserConverter {
public static UserDTO toDTO(UserDO userDO) {
UserDTO userDTO = new UserDTO();
userDTO.setAge(userDO.getAge());
// 问题 1: 自己赋值给自己
userDTO.setName(userDTO.getName());
return userDTO;
}
public static class UserDO {
private String name;
private Integer age;
// 问题 2: 新增字段未赋值
private String address;
}
public static class UserDTO {
private String name;
private Integer age;
}
}
反例2:
public class UserBeanCopyConvert {
public UserDTO toDTO(UserDO userDO) {
UserDTO userDTO = new UserDTO();
// 用反射复制不同类型对象.
// 1. 重构不友好, 当我要删除或修改 UserDO 的字段时, 无法得知该字段是否通过反射被其他字段依赖
BeanUtils.copyProperties(userDO, userDTO);
return userDTO;
}
}
-
使用 Mapstruct
-
未映射字段的处理策略,在编译期发现映射问题;
-
复用工具,方便字段类型转化;
-
生成 spring Component 注解,通过 spring 管理;
-
等等其他特性;
(
componentModel = "spring",
unmappedSourcePolicy = ReportingPolicy.ERROR,
unmappedTargetPolicy = ReportingPolicy.ERROR,
// convert 逻辑依赖 DateUtil 做日期转化
uses = DateUtil.class
)
public interface UserConvertor {
UserDTO toUserDTO(UserDO userDO);
class UserDO {
private String name;
private Integer age;
//private String address;
private Date birthDay;
}
class UserDTO {
private String name;
private Integer age;
private String birthDay;
}
}
public class DateUtil {
public static String format(Date date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
return simpleDateFormat.format(date);
}
}
使用示例:
public class UserService {
private final UserDao userDao;
private final UserCovertor userCovertor;
public UserDTO getUser(String userId){
UserDO userDO = userDao.getById(userId);
return userCovertor.toUserDTO(userDO);
}
}
编译期校验:
(
value = "org.mapstruct.ap.MappingProcessor",
date = "2023-12-18T20:17:00+0800",
comments = "version: 1.3.1.Final, compiler: javac, environment: Java 11.0.12 (GraalVM Community)"
)
public class UserConvertorImpl implements UserConvertor {
public UserDTO toUserDTO(UserDO userDO) {
if ( userDO == null ) {
return null;
}
UserDTO userDTO = new UserDTO();
userDTO.setName( userDO.getName() );
userDTO.setAge( userDO.getAge() );
userDTO.setBirthDay( DateUtil.format( userDO.getBirthDay() ) );
return userDTO;
}
}
-
熟练使用线程安全类
public class ConcurrentHashMapExample {
private Map
map = new ConcurrentHashMap();
public void appendIfExists(String key, String suffix) {
String value = map.get(key);
if (value != null) {
map.put(key, value + suffix);
}
}
}
正例:
public class ConcurrentHashMapExample {
private Map
map = new ConcurrentHashMap();
public void append(String key, String suffix) {
// 使用 computeIfPresent 原子操作
map.computeIfPresent(key, (k, v) -> v + suffix);
}
}
-
保证变更的原子性
public class NoAtomicDiamondParser {
private volatile int start;
private volatile int end;
public NoAtomicDiamondParser() {
Diamond.addListener("dataId", "groupId", new ManagerListenerAdapter() {
public void receiveConfigInfo(String s) {
JSONObject jsonObject = JSON.parseObject(s);
start = jsonObject.getIntValue("start");
end = jsonObject.getIntValue("end");
}
});
}
}
public class MyController{
private final NoAtomicDiamondParser noAtomicDiamondParser;
public void handleRange(){
// end 读取的旧值, start 读取的新值, start 可能大于 end
int end = noAtomicDiamondParser.getEnd();
int start = noAtomicDiamondParser.getStart();
}
}
正例:
public class AtomicDiamondParser {
private volatile Range range;
public AtomicDiamondParser() {
Diamond.addListener("dataId", "groupId", new ManagerListenerAdapter() {
public void receiveConfigInfo(String s) {
range = JSON.parseObject(s, Range.class);
}
});
}
public static class Range {
private int start;
private int end;
}
}
public class MyController {
private final AtomicDiamondParser atomicDiamondParser;
public void handleRange() {
Range range = atomicDiamondParser.getRange();
System.out.println(range.getStart());
System.out.println(range.getEnd());
}
}
-
使用不可变对象
public class AtomicDiamondParser {
private volatile Range range;
public AtomicDiamondParser() {
Diamond.addListener("dataId", "groupId", new ManagerListenerAdapter() {
public void receiveConfigInfo(String s) {
JSONObject jsonObject = JSON.parseObject(s);
int start = jsonObject.getIntValue("start");
int end = jsonObject.getIntValue("end");
range = new Range(start, end);
}
});
}
// lombok 注解会保证 Range 类的不变性
public static class Range {
private int start;
private int end;
}
}
-
正确性优先于性能
class Foo {
// 缺少 volatile 关键字
private Helper helper = null;
public Helper getHelper() {
if (helper == null)
synchronized(this) {
if (helper == null)
helper = new Helper();
}
return helper;
}
}
在上述例子中,在 helper 字段上增加 volatile 关键字,能够在 java 5 及之后的版本中保证线程安全。
class Foo {
private volatile Helper helper = null;
public Helper getHelper() {
if (helper == null)
synchronized(this) {
if (helper == null)
helper = new Helper();
}
return helper;
}
}
正例3(推荐):
class Foo {
private Helper helper = null;
public synchronized Helper getHelper() {
if (helper == null)
helper = new Helper();
}
return helper;
}
并不严谨的 Diamond Parser:
/**
* 省略异常处理等其他逻辑
*/
public class DiamondParser {
// 缺少 volatile 关键字
private Config config;
public DiamondParser() {
Diamond.addListener("dataId", "groupId", new ManagerListenerAdapter() {
public void receiveConfigInfo(String s) {
config = JSON.parseObject(s, Config.class);
}
});
}
public static class Config {
private String name;
}
}
这种 Diamond 写法可能从来没有发生过线上问题,但这种写法也确实是不符合 JVM 线程安全原则。未来某一天你的代码跑在另一个 JVM 实现上,可能就有问题了。
线程池使用不当
public class ThreadPoolExample {
// 没有任何限制的线程池, 使用起来很方便, 但当一波请求高峰到达时, 可能会创建大量线程, 导致系统崩溃
private static Executor executor = Executors.newCachedThreadPool();
}
反例 2:
public class StreamParallelExample {
public ListString> batchQuery(ListString> ids){
// 看上去很优雅, 但 ForkJoinPool 的队列是没有大小限制的, 并且线程数量很少, 如果 ids 列表很大可能导致 OOM
// parallelStream 更适合计算密集型任务, 不要在任务中做远程调用
return ids.parallelStream()
.map(this::queryFromRemote)
.collect(Collectors.toList());
}
private String queryFromRemote(String id){
// 从远程查询
}
}
-
手动创建线程池
public class ManualCreateThreadPool {
// 手动创建资源有限的线程池
private Executor executor = new ThreadPoolExecutor(10, 10, 1, TimeUnit.MINUTES, new ArrayBlockingQueue(1000),
new ThreadFactoryBuilder().setNameFormat("work-%d").build());
}
public class DuplicatedExceptionHandlerExample {
private UserService userService;
public User query(String id) {
try {
return userService.query(id);
} catch (Exception e) {
log.error("query error, userId: {}", id, e);
return null;
}
}
public User create(String id) {
try {
return userService.create(id);
} catch (Exception e) {
log.error("query error, userId: {}", id, e);
return null;
}
}
}
反例 2:
public class ExceptionShouldLogOrThrowExample {
private UserService userService;
public User query(String id) {
try {
return userService.query(id);
} catch (Exception e) {
// 异常被吞并, 问题被隐藏
return null;
}
}
public User create(String id) {
try {
return userService.create(id);
} catch (Exception e) {
// 堆栈丢失, 后续难以定位问题
log.error("query error, userId: {}, error: {}", id,e.getMessage() );
return null;
}
}
}
反例 3:
public class OpenAPIService {
public void handle(){
// HSF 服务对外抛出 client 中未定义的异常, 调用方反序列化失败
throw new InternalSystemException("");
}
}
-
通过 AOP 统一异常处理
-
避免未知异常抛给调用方, 将未知异常转为 Result 或者通用异常类型 -
统一异常日志的打印和监控
-
处理 Checked Exception
-
Try catch 线程逻辑
public class ThreadNotTryCatch {
private final ExecutorService executorService;
public void handle() {
executorService.submit(new Runnable() {
public void run() {
// 未捕获异常, 线程直接退出, 异常信息丢失
remoteInvoke();
}
});
}
}
正例:
4j
public class ThreadNotTryCatch {
private final ExecutorService executorService;
public void handle() {
executorService.submit(new Runnable() {
public void run() {
try {
remoteInvoke();
} catch (Exception e) {
log.error("handle failed", e);
}
}
});
}
}
-
特殊异常的处理
public class InterruptedExceptionExample {
private ExecutorService executorService = Executors.newSingleThreadExecutor();
public void handleWithTimeout() throws InterruptedException {
Future> future = executorService.submit(() -> {
try {
// sleep 模拟处理逻辑
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("interrupted");
}
System.out.println("continue task");
// 异常被忽略, 继续处理
});
// 等待任务结果, 如果超过 500ms 则中断
Thread.sleep(500);
if (!future.isDone()) {
System.out.println("cancel");
future.cancel(true);
}
}
}
-
避免 catch Error
public class ErrorExample {
private Date start;
private Date end;
public synchronized void update(long start, long end) {
if (start > end) {
throw new IllegalArgumentException("start after end");
}
this.start = new Date(start);
// 如果 new Date(end) 发生 OOM, start 有可能大于 end
this.end = new Date(end);
}
}
-
反例 1: SpringContext 作为静态变量
public class SpringContextUtils {
private static ApplicationContext applicationContext;
public SpringContextUtils(ApplicationContext context) {
applicationContext = context;
}
}
public class UserController {
public void handle(){
MyService bean = SpringContextUtils.getApplicationContext().getBean(MyService.class);
}
}
反例 2: Switch 在 Spring Bean 中注册, 但通过静态方式读取
public class SwitchConfig {
public void init() {
SwitchManager.register("appName", MySwitch.class);
}
public static class MySwitch {
"config", level = Switch.Level.p1) (des =
public static String config;
}
}
public class UserController{
public String getConfig(){
// UserController 和 SwitchConfig 类没有依赖关系, MySwitch.config 可能还没有初始化
return MySwitch.config;
}
}
通过 SpringBeanFactory 保证初始化顺序:
public class PreInitializer implements BeanFactoryPostProcessor, PriorityOrdered {
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}
public void postProcessBeanFactory(
ConfigurableListableBeanFactory beanFactory) throws BeansException {
try {
SwitchManager.init(应用名, 开关类.class);
} catch (SwitchCenterException e) {
// 此处抛错最好阻断程序启动,避免开关读不到持久值引发问题
} catch (SwitchCenterError e) {
System.exit(1);
}
}
}
public class SpringContextUtilPostProcessor implements BeanFactoryPostProcessor, PriorityOrdered, ApplicationContextAware {
private ApplicationContext applicationContext;
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
throws BeansException {
SpringContextUtils.setApplicationContext(applicationContext);
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
-
LoadingCache 代替全局 Map
public class MetaInfoManager {
// 对于少量的元数据来说, 放到内存中似乎并无大碍, 但如果后续元数据量增大, 则大量对象则内存中无法释放, 导致内存泄漏
private MapString, MetaInfo> cache = new HashMap();
public MetaInfo getMetaInfo(String id) {
return cache.computeIfAbsent(id, k -> loadFromRemote(id));
}
private LoadingCacheString, MetaInfo> loadingCache = CacheBuilder.newBuilder()
// loadingCache 设置最大 size 或者过期时间, 能够限制缓存条目的数量
.maximumSize(1000)
.build(new CacheLoaderString, MetaInfo>() {
public MetaInfo load(String key) throws Exception {
return loadFromRemote(key);
}
});
public MetaInfo getMetaInfoFromLoadingCache(String id) {
return loadingCache.getUnchecked(id);
}
private MetaInfo loadFromRemote(String id) {
return null;
}
public static class MetaInfo {
private String id;
private String name;
}
}
-
谨慎使用运行时类生成技术
-
使用 Try With Resource
public class TryWithResourceExample {
public static void main(String[] args) throws IOException {
try (InputStream in = Files.newInputStream(Paths.get(""))) {
// read
}
}
}
public class URLExample {
public void handle(URL a, URL b) {
if (Objects.equals(a, b)) {
}
}
}
反例 2:
public class URLMapExample {
private static final Map
urlObjectMap = new HashMap();
}
循环远程调用:
public class HSFLoopInvokeExample {
@HSFConsumer
private UserService userService;
public List
batchQuery(List ids){ // 使用批量接口或者限制批量大小
return ids.stream()
.map(userService::getUser)
.collect(Collectors.toList());
}
}
-
了解常见性能指标&瓶颈
-
使用专业性能测试工具估性能
public class ManualPerformanceTest {
public void testPerformance() {
long start = System.currentTimeMillis();
for (int i = 0; i 1000; i++) {
// 这里 mutiply 没有任何副作用, 有可能被优化之后被干掉
mutiply(10, 10);
}
System.out.println("avg rt: " + (System.currentTimeMillis() - start) / 1000);
}
private int mutiply(int a, int b) {
return a * b;
}
}
正例:
5, time = 1, timeUnit = TimeUnit.SECONDS) (iterations =
5, time = 1, timeUnit = TimeUnit.SECONDS) (iterations =
3) (
(Mode.AverageTime)
(TimeUnit.NANOSECONDS)
public class JMHExample {
public void testPerformance(Blackhole bh) {
bh.consume(mutiply(10, 10));
}
private int mutiply(int a, int b) {
return a * b;
}
}
-
注意事务注解失效的场景
public class TransactionNotWork {
public void doTheThing() {
actuallyDoTheThing();
}
public void actuallyDoTheThing() {
}
}
参考资料:
-
Null:价值 10 亿美元的错误: https://www.infoq.cn/article/uyyos0vgetwcgmo1ph07 -
双重检查锁失效声明: https://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html -
每个程序员都应该知道的延迟数字: https://colin-scott.github.io/personal_website/research/interactive_latency.html
0 条评论