Java日常開發(fā)的21個(gè)坑,你踩過幾個(gè)?
前言
最近看了極客時(shí)間的《Java業(yè)務(wù)開發(fā)常見錯(cuò)誤100例》,再結(jié)合平時(shí)踩的一些代碼坑,寫寫總結(jié),希望對大家有幫助,感謝閱讀~
1. 六類典型空指針問題
包裝類型的空指針問題 級聯(lián)調(diào)用的空指針問題 Equals方法左邊的空指針問題 ConcurrentHashMap 這樣的容器不支持 Key 和 Value 為 null。 集合,數(shù)組直接獲取元素 對象直接獲取屬性
1.1包裝類型的空指針問題
public?class?NullPointTest?{
????public?static?void?main(String[]?args)?throws?InterruptedException?{
????????System.out.println(testInteger(null));
????}
????private?static?Integer?testInteger(Integer?i)?{
????????return?i?+?1;??//包裝類型,傳參可能為null,直接計(jì)算,則會導(dǎo)致空指針問題
????}
}
1.2 級聯(lián)調(diào)用的空指針問題
public?class?NullPointTest?{
????public?static?void?main(String[]?args)?{
???????//fruitService.getAppleService()?可能為空,會導(dǎo)致空指針問題
????????fruitService.getAppleService().getWeight().equals("OK");
????}
}
1.3 Equals方法左邊的空指針問題
public?class?NullPointTest?{
????public?static?void?main(String[]?args)?{
????????String?s?=?null;
????????if?(s.equals("666"))?{?//s可能為空,會導(dǎo)致空指針問題
????????????System.out.println("公眾號:撿田螺的小男孩,666");
????????}
????}
}
1.4 ConcurrentHashMap 這樣的容器不支持 Key,Value 為 null。
public?class?NullPointTest?{
????public?static?void?main(String[]?args)?{
????????Map?map?=?new?ConcurrentHashMap<>();
????????String?key?=?null;
????????String?value?=?null;
????????map.put(key,?value);
????}
}
1.5 ?集合,數(shù)組直接獲取元素
public?class?NullPointTest?{
????public?static?void?main(String[]?args)?{
????????int?[]?array=null;
????????List?list?=?null;
????????System.out.println(array[0]);?//空指針異常
????????System.out.println(list.get(0));?//空指針一場
????}
}
1.6 對象直接獲取屬性
public?class?NullPointTest?{
????public?static?void?main(String[]?args)?{
????????User?user=null;
????????System.out.println(user.getAge());?//空指針異常
????}
}
2. 日期YYYY格式設(shè)置的坑
日常開發(fā),經(jīng)常需要對日期格式化,但是呢,年份設(shè)置為YYYY大寫的時(shí)候,是有坑的哦。
反例:
Calendar?calendar?=?Calendar.getInstance();
calendar.set(2019,?Calendar.DECEMBER,?31);
Date?testDate?=?calendar.getTime();
SimpleDateFormat?dtf?=?new?SimpleDateFormat("YYYY-MM-dd");
System.out.println("2019-12-31?轉(zhuǎn)?YYYY-MM-dd?格式后?"?+?dtf.format(testDate));
運(yùn)行結(jié)果:
2019-12-31?轉(zhuǎn)?YYYY-MM-dd?格式后?2020-12-31
「解析:」
為什么明明是2019年12月31號,就轉(zhuǎn)了一下格式,就變成了2020年12月31號了?因?yàn)閅YYY是基于周來計(jì)算年的,它指向當(dāng)天所在周屬于的年份,一周從周日開始算起,周六結(jié)束,只要本周跨年,那么這一周就算下一年的了。正確姿勢是使用yyyy格式。

正例:
Calendar?calendar?=?Calendar.getInstance();
calendar.set(2019,?Calendar.DECEMBER,?31);
Date?testDate?=?calendar.getTime();
SimpleDateFormat?dtf?=?new?SimpleDateFormat("yyyy-MM-dd");
System.out.println("2019-12-31?轉(zhuǎn)?yyyy-MM-dd?格式后?"?+?dtf.format(testDate));
3.金額數(shù)值計(jì)算精度的坑
看下這個(gè)浮點(diǎn)數(shù)計(jì)算的例子吧:
public?class?DoubleTest?{
????public?static?void?main(String[]?args)?{
????????System.out.println(0.1+0.2);
????????System.out.println(1.0-0.8);
????????System.out.println(4.015*100);
????????System.out.println(123.3/100);
????????double?amount1?=?3.15;
????????double?amount2?=?2.10;
????????if?(amount1?-?amount2?==?1.05){
????????????System.out.println("OK");
????????}
????}
}
運(yùn)行結(jié)果:
0.30000000000000004
0.19999999999999996
401.49999999999994
1.2329999999999999
可以發(fā)現(xiàn),結(jié)算結(jié)果跟我們預(yù)期不一致,其實(shí)是因?yàn)橛?jì)算機(jī)是以二進(jìn)制存儲數(shù)值的,對于浮點(diǎn)數(shù)也是。對于計(jì)算機(jī)而言,0.1無法精確表達(dá),這就是為什么浮點(diǎn)數(shù)會導(dǎo)致精確度缺失的。因此,「金額計(jì)算,一般都是用BigDecimal 類型」
對于以上例子,我們改為BigDecimal,再看看運(yùn)行效果:
System.out.println(new?BigDecimal(0.1).add(new?BigDecimal(0.2)));
System.out.println(new?BigDecimal(1.0).subtract(new?BigDecimal(0.8)));
System.out.println(new?BigDecimal(4.015).multiply(new?BigDecimal(100)));
System.out.println(new?BigDecimal(123.3).divide(new?BigDecimal(100)));
運(yùn)行結(jié)果:
0.3000000000000000166533453693773481063544750213623046875
0.1999999999999999555910790149937383830547332763671875
401.49999999999996802557689079549163579940795898437500
1.232999999999999971578290569595992565155029296875
發(fā)現(xiàn)結(jié)果還是不對,「其實(shí)」,使用 BigDecimal 表示和計(jì)算浮點(diǎn)數(shù),必須使用「字符串的構(gòu)造方法」來初始化 BigDecimal,正例如下:
public?class?DoubleTest?{
????public?static?void?main(String[]?args)?{
????????System.out.println(new?BigDecimal("0.1").add(new?BigDecimal("0.2")));
????????System.out.println(new?BigDecimal("1.0").subtract(new?BigDecimal("0.8")));
????????System.out.println(new?BigDecimal("4.015").multiply(new?BigDecimal("100")));
????????System.out.println(new?BigDecimal("123.3").divide(new?BigDecimal("100")));
????}
}
在進(jìn)行金額計(jì)算,使用BigDecimal的時(shí)候,我們還需要「注意BigDecimal的幾位小數(shù)點(diǎn),還有它的八種舍入模式哈」。
4. FileReader默認(rèn)編碼導(dǎo)致亂碼問題
看下這個(gè)例子:
public?class?FileReaderTest?{
????public?static?void?main(String[]?args)?throws?IOException?{
????????Files.deleteIfExists(Paths.get("jay.txt"));
????????Files.write(Paths.get("jay.txt"),?"你好,撿田螺的小男孩".getBytes(Charset.forName("GBK")));
????????System.out.println("系統(tǒng)默認(rèn)編碼:"+Charset.defaultCharset());
????????char[]?chars?=?new?char[10];
????????String?content?=?"";
????????try?(FileReader?fileReader?=?new?FileReader("jay.txt"))?{
????????????int?count;
????????????while?((count?=?fileReader.read(chars))?!=?-1)?{
????????????????content?+=?new?String(chars,?0,?count);
????????????}
????????}
????????System.out.println(content);
????}
}
運(yùn)行結(jié)果:
系統(tǒng)默認(rèn)編碼:UTF-8
???,???????С?к?
從運(yùn)行結(jié)果,可以知道,系統(tǒng)默認(rèn)編碼是utf8,demo中讀取出來,出現(xiàn)亂碼了。為什么呢?
?FileReader 是以當(dāng)「前機(jī)器的默認(rèn)字符集」來讀取文件的,如果希望指定字符集的話,需要直接使用 InputStreamReader 和 FileInputStream。
?
正例如下:
public?class?FileReaderTest?{
????public?static?void?main(String[]?args)?throws?IOException?{
????????Files.deleteIfExists(Paths.get("jay.txt"));
????????Files.write(Paths.get("jay.txt"),?"你好,撿田螺的小男孩".getBytes(Charset.forName("GBK")));
????????System.out.println("系統(tǒng)默認(rèn)編碼:"+Charset.defaultCharset());
????????char[]?chars?=?new?char[10];
????????String?content?=?"";
????????try?(FileInputStream?fileInputStream?=?new?FileInputStream("jay.txt");
?????????????InputStreamReader?inputStreamReader?=?new?InputStreamReader(fileInputStream,?Charset.forName("GBK")))?{
????????????int?count;
????????????while?((count?=?inputStreamReader.read(chars))?!=?-1)?{
????????????????content?+=?new?String(chars,?0,?count);
????????????}
????????}
????????System.out.println(content);
????}
}
5. Integer緩存的坑
public?class?IntegerTest?{
????public?static?void?main(String[]?args)?{
????????Integer?a?=?127;
????????Integer?b?=?127;
????????System.out.println("a==b:"+?(a?==?b));
????????
????????Integer?c?=?128;
????????Integer?d?=?128;
????????System.out.println("c==d:"+?(c?==?d));
????}
}
運(yùn)行結(jié)果:
a==b:true
c==d:false
為什么Integer值如果是128就不相等了呢?「編譯器會把 Integer a = 127 轉(zhuǎn)換為 Integer.valueOf(127)。」 我們看下源碼。
public?static?Integer?valueOf(int?i)?{
??????if?(i?>=?IntegerCache.low?&&?i?<=?IntegerCache.high)
??????????return?IntegerCache.cache[i?+?(-IntegerCache.low)];
??????return?new?Integer(i);
?}
可以發(fā)現(xiàn),i在一定范圍內(nèi),是會返回緩存的。
?默認(rèn)情況下呢,這個(gè)緩存區(qū)間就是[-128, 127],所以我們業(yè)務(wù)日常開發(fā)中,如果涉及Integer值的比較,需要注意這個(gè)坑哈。還有呢,設(shè)置 JVM 參數(shù)加上 -XX:AutoBoxCacheMax=1000,是可以調(diào)整這個(gè)區(qū)間參數(shù)的,大家可以自己試一下哈
?
6. static靜態(tài)變量依賴spring實(shí)例化變量,可能導(dǎo)致初始化出錯(cuò)
之前看到過類似的代碼。靜態(tài)變量依賴于spring容器的bean。
?private?static?SmsService?smsService?=?SpringContextUtils.getBean(SmsService.class);
這個(gè)靜態(tài)的smsService有可能獲取不到的,因?yàn)轭惣虞d順序不是確定的,正確的寫法可以這樣,如下:
?private?static?SmsService??smsService?=null;
?
?//使用到的時(shí)候采取獲取
?public?static?SmsService?getSmsService(){
???if(smsService==null){
??????smsService?=?SpringContextUtils.getBean(SmsService.class);
???}
???return?smsService;
?}
7. 使用ThreadLocal,線程重用導(dǎo)致信息錯(cuò)亂的坑
使用ThreadLocal緩存信息,有可能出現(xiàn)信息錯(cuò)亂的情況。看下下面這個(gè)例子吧。
private?static?final?ThreadLocal?currentUser?=?ThreadLocal.withInitial(()?->?null);
@GetMapping("wrong")
public?Map?wrong(@RequestParam("userId")?Integer?userId)?{
????//設(shè)置用戶信息之前先查詢一次ThreadLocal中的用戶信息
????String?before??=?Thread.currentThread().getName()?+?":"?+?currentUser.get();
????//設(shè)置用戶信息到ThreadLocal
????currentUser.set(userId);
????//設(shè)置用戶信息之后再查詢一次ThreadLocal中的用戶信息
????String?after??=?Thread.currentThread().getName()?+?":"?+?currentUser.get();
????//匯總輸出兩次查詢結(jié)果
????Map?result?=?new?HashMap();
????result.put("before",?before);
????result.put("after",?after);
????return?result;
}
按理說,每次獲取的before應(yīng)該都是null,但是呢,程序運(yùn)行在 Tomcat 中,執(zhí)行程序的線程是 Tomcat 的工作線程,而 Tomcat 的工作線程是基于線程池的。
?線程池會重用固定的幾個(gè)線程,一旦線程重用,那么很可能首次從 ThreadLocal 獲取的值是之前其他用戶的請求遺留的值。這時(shí),ThreadLocal 中的用戶信息就是其他用戶的信息。
?
把tomcat的工作線程設(shè)置為1
server.tomcat.max-threads=1
用戶1,請求過來,會有以下結(jié)果,符合預(yù)期:

用戶2請求過來,會有以下結(jié)果,「不符合預(yù)期」:

因此,使用類似 ThreadLocal 工具來存放一些數(shù)據(jù)時(shí),需要特別注意在代碼運(yùn)行完后,顯式地去清空設(shè)置的數(shù)據(jù),正例如下:
@GetMapping("right")
public?Map?right(@RequestParam("userId")?Integer?userId)?{
????String?before??=?Thread.currentThread().getName()?+?":"?+?currentUser.get();
????currentUser.set(userId);
????try?{
????????String?after?=?Thread.currentThread().getName()?+?":"?+?currentUser.get();
????????Map?result?=?new?HashMap();
????????result.put("before",?before);
????????result.put("after",?after);
????????return?result;
????}?finally?{
????????//在finally代碼塊中刪除ThreadLocal中的數(shù)據(jù),確保數(shù)據(jù)不串
????????currentUser.remove();
????}
}
8. 疏忽switch的return和break
這一點(diǎn)嚴(yán)格來說,應(yīng)該不算坑,但是呢,大家寫代碼的時(shí)候,有些朋友容易疏忽了。直接看例子吧
/*
?*?關(guān)注公眾號:
?*?撿田螺的小男孩
?*/
public?class?SwitchTest?{
????public?static?void?main(String[]?args)?throws?InterruptedException?{
????????System.out.println("testSwitch結(jié)果是:"+testSwitch("1"));
????}
????private?static?String?testSwitch(String?key)?{
????????switch?(key)?{
????????????case?"1":
????????????????System.out.println("1");
????????????case?"2":
????????????????System.out.println(2);
????????????????return?"2";
????????????case?"3":
????????????????System.out.println("3");
????????????default:
????????????????System.out.println("返回默認(rèn)值");
????????????????return?"4";
????????}
????}
}
輸出結(jié)果:
測試switch
1
2
testSwitch結(jié)果是:2
switch 是會「沿著case一直往下匹配的,知道遇到return或者break。」 所以,在寫代碼的時(shí)候留意一下,是不是你要的結(jié)果。
9. Arrays.asList的幾個(gè)坑
9.1 基本類型不能作為 Arrays.asList方法的參數(shù),否則會被當(dāng)做一個(gè)參數(shù)。
public?class?ArrayAsListTest?{
????public?static?void?main(String[]?args)?{
????????int[]?array?=?{1,?2,?3};
????????List?list?=?Arrays.asList(array);
????????System.out.println(list.size());
????}
}
運(yùn)行結(jié)果:
1
Arrays.asList源碼如下:
public?static??List?asList(T...?a)?{
????return?new?ArrayList<>(a);
}
9.2 Arrays.asList 返回的 List 不支持增刪操作。
public?class?ArrayAsListTest?{
????public?static?void?main(String[]?args)?{
????????String[]?array?=?{"1",?"2",?"3"};
????????List?list?=?Arrays.asList(array);
????????list.add("5");
????????System.out.println(list.size());
????}
}
運(yùn)行結(jié)果:
Exception?in?thread?"main"?java.lang.UnsupportedOperationException
?at?java.util.AbstractList.add(AbstractList.java:148)
?at?java.util.AbstractList.add(AbstractList.java:108)
?at?object.ArrayAsListTest.main(ArrayAsListTest.java:11)
Arrays.asList 返回的 List 并不是我們期望的 java.util.ArrayList,而是 Arrays 的內(nèi)部類 ArrayList。內(nèi)部類的ArrayList沒有實(shí)現(xiàn)add方法,而是父類的add方法的實(shí)現(xiàn),是會拋出異常的呢。
9.3 使用Arrays.asLis的時(shí)候,對原始數(shù)組的修改會影響到我們獲得的那個(gè)List
public?class?ArrayAsListTest?{
????public?static?void?main(String[]?args)?{
????????String[]?arr?=?{"1",?"2",?"3"};
????????List?list?=?Arrays.asList(arr);
????????arr[1]?=?"4";
????????System.out.println("原始數(shù)組"+Arrays.toString(arr));
????????System.out.println("list數(shù)組"?+?list);
????}
}
運(yùn)行結(jié)果:
原始數(shù)組[1,?4,?3]
list數(shù)組[1,?4,?3]
從運(yùn)行結(jié)果可以看到,原數(shù)組改變,Arrays.asList轉(zhuǎn)化來的list也跟著改變啦,大家使用的時(shí)候要注意一下哦,可以用new ArrayList(Arrays.asList(arr))包一下的。
10. ArrayList.toArray() 強(qiáng)轉(zhuǎn)的坑
public?class?ArrayListTest?{
????public?static?void?main(String[]?args)?{
????????List?list?=?new?ArrayList(1);
????????list.add("公眾號:撿田螺的小男孩");
????????String[]?array21?=?(String[])list.toArray();//類型轉(zhuǎn)換異常
????}
}
因?yàn)榉祷氐氖荗bject類型,Object類型數(shù)組強(qiáng)轉(zhuǎn)String數(shù)組,會發(fā)生ClassCastException。解決方案是,使用toArray()重載方法toArray(T[] a)
String[]?array1?=?list.toArray(new?String[0]);//可以正常運(yùn)行
11. 異常使用的幾個(gè)坑
11.1 不要弄丟了你的堆棧異常信息
public?void?wrong1(){
????try?{
????????readFile();
????}?catch?(IOException?e)?{
????????//沒有把異常e取出來,原始異常信息丟失??
????????throw?new?RuntimeException("系統(tǒng)忙請稍后再試");
????}
}
public?void?wrong2(){
????try?{
????????readFile();
????}?catch?(IOException?e)?{
????????//只保留了異常消息,棧沒有記錄啦
????????log.error("文件讀取錯(cuò)誤,?{}",?e.getMessage());
????????throw?new?RuntimeException("系統(tǒng)忙請稍后再試");
????}
}
正確的打印方式,應(yīng)該醬紫
public?void?right(){
????try?{
????????readFile();
????}?catch?(IOException?e)?{
????????//把整個(gè)IO異常都記錄下來,而不是只打印消息
????????log.error("文件讀取錯(cuò)誤",?e);
????????throw?new?RuntimeException("系統(tǒng)忙請稍后再試");
????}
}
11.2 不要把異常定義為靜態(tài)變量
public?void?testStaticExeceptionOne{
????try?{
????????exceptionOne();
????}?catch?(Exception?ex)?{
????????log.error("exception?one?error",?ex);
????}
????try?{
????????exceptionTwo();
????}?catch?(Exception?ex)?{
????????log.error("exception?two?error",?ex);
????}
}
private?void?exceptionOne()?{
????//這里有問題
????throw?Exceptions.ONEORTWO;
}
private?void?exceptionTwo()?{
????//這里有問題
????throw?Exceptions.ONEORTWO;
}
exceptionTwo拋出的異常,很可能是 exceptionOne的異常哦。正確使用方法,應(yīng)該是new 一個(gè)出來。
private?void?exceptionTwo()?{
????throw?new?BusinessException("業(yè)務(wù)異常",?0001);
}
11.3 生產(chǎn)環(huán)境不要使用e.printStackTrace();
public?void?wrong(){
????try?{
????????readFile();
????}?catch?(IOException?e)?{
???????//生產(chǎn)環(huán)境別用它
????????e.printStackTrace();
????}
}
因?yàn)樗加锰鄡?nèi)存,造成鎖死,并且,日志交錯(cuò)混合,也不易讀。正確使用如下:
log.error("異常日志正常打印方式",e);
11.4 線程池提交過程中,出現(xiàn)異常怎么辦?
public?class?ThreadExceptionTest?{
????public?static?void?main(String[]?args)?{
????????ExecutorService?executorService?=?Executors.newFixedThreadPool(10);
????????IntStream.rangeClosed(1,?10).forEach(i?->?executorService.submit(()->?{
????????????????????if?(i?==?5)?{
????????????????????????System.out.println("發(fā)生異常啦");
????????????????????????throw?new?RuntimeException("error");
????????????????????}
????????????????????System.out.println("當(dāng)前執(zhí)行第幾:"?+?Thread.currentThread().getName()?);
????????????????}
????????));
????????executorService.shutdown();
????}
}
運(yùn)行結(jié)果:
當(dāng)前執(zhí)行第幾:pool-1-thread-1
當(dāng)前執(zhí)行第幾:pool-1-thread-2
當(dāng)前執(zhí)行第幾:pool-1-thread-3
當(dāng)前執(zhí)行第幾:pool-1-thread-4
發(fā)生異常啦
當(dāng)前執(zhí)行第幾:pool-1-thread-6
當(dāng)前執(zhí)行第幾:pool-1-thread-7
當(dāng)前執(zhí)行第幾:pool-1-thread-8
當(dāng)前執(zhí)行第幾:pool-1-thread-9
當(dāng)前執(zhí)行第幾:pool-1-thread-10
可以發(fā)現(xiàn),如果是使用submit方法提交到線程池的異步任務(wù),異常會被吞掉的,所以在日常發(fā)現(xiàn)中,如果會有可預(yù)見的異常,可以采取這幾種方案處理:
1.在任務(wù)代碼try/catch捕獲異常 2.通過Future對象的get方法接收拋出的異常,再處理 3.為工作者線程設(shè)置UncaughtExceptionHandler,在uncaughtException方法中處理異常 4.重寫ThreadPoolExecutor的afterExecute方法,處理傳遞的異常引用
11.5 finally重新拋出的異常也要注意啦
public?void?wrong()?{
????try?{
????????log.info("try");
????????//異常丟失
????????throw?new?RuntimeException("try");
????}?finally?{
????????log.info("finally");
????????throw?new?RuntimeException("finally");
????}
}
一個(gè)方法是不會出現(xiàn)兩個(gè)異常的呢,所以finally的異常會把try的「異常覆蓋」。正確的使用方式應(yīng)該是,finally 代碼塊「負(fù)責(zé)自己的異常捕獲和處理」。
public?void?right()?{
????try?{
????????log.info("try");
????????throw?new?RuntimeException("try");
????}?finally?{
????????log.info("finally");
????????try?{
????????????throw?new?RuntimeException("finally");
????????}?catch?(Exception?ex)?{
????????????log.error("finally",?ex);
????????}
????}
}
12.JSON序列化,Long類型被轉(zhuǎn)成Integer類型!
public?class?JSONTest?{
????public?static?void?main(String[]?args)?{
????????Long?idValue?=?3000L;
????????Map?data?=?new?HashMap<>(2);
????????data.put("id",?idValue);
????????data.put("name",?"撿田螺的小男孩");
????????Assert.assertEquals(idValue,?(Long)?data.get("id"));
????????String?jsonString?=?JSON.toJSONString(data);
????????//?反序列化時(shí)Long被轉(zhuǎn)為了Integer
????????Map?map?=?JSON.parseObject(jsonString,?Map.class);
????????Object?idObj?=?map.get("id");
????????System.out.println("反序列化的類型是否為Integer:"+(idObj?instanceof?Integer));
????????Assert.assertEquals(idValue,?(Long)?idObj);
????}
}
「運(yùn)行結(jié)果:」
Exception?in?thread?"main"?反序列化的類型是否為Integer:true
java.lang.ClassCastException:?java.lang.Integer?cannot?be?cast?to?java.lang.Long
?at?object.JSONTest.main(JSONTest.java:24)
?「注意啦」,序列化為Json串后,Josn串是沒有Long類型呢。而且反序列化回來如果也是Object接收,數(shù)字小于Interger最大值的話,給轉(zhuǎn)成Integer啦!
?
13. 使用Executors聲明線程池,newFixedThreadPool的OOM問題
ExecutorService?executor?=?Executors.newFixedThreadPool(10);
????????for?(int?i?=?0;?i?????????????executor.execute(()?->?{
????????????????try?{
????????????????????Thread.sleep(10000);
????????????????}?catch?(InterruptedException?e)?{
????????????????????//do?nothing
????????????????}
????????????});
????????}
「IDE指定JVM參數(shù):-Xmx8m -Xms8m :」

運(yùn)行結(jié)果:

我們看下源碼,其實(shí)newFixedThreadPool使用的是無界隊(duì)列!
public?static?ExecutorService?newFixedThreadPool(int?nThreads)?{
????return?new?ThreadPoolExecutor(nThreads,?nThreads,
??????????????????????????????????0L,?TimeUnit.MILLISECONDS,
??????????????????????????????????new?LinkedBlockingQueue());
}
public?class?LinkedBlockingQueue?extends?AbstractQueue
????????implements?BlockingQueue,?java.io.Serializable?{
????...
????/**
?????*?Creates?a?{@code?LinkedBlockingQueue}?with?a?capacity?of
?????*?{@link?Integer#MAX_VALUE}.
?????*/
????public?LinkedBlockingQueue()?{
????????this(Integer.MAX_VALUE);
????}
...
}
?newFixedThreadPool線程池的核心線程數(shù)是固定的,它使用了近乎于無界的LinkedBlockingQueue阻塞隊(duì)列。當(dāng)核心線程用完后,任務(wù)會入隊(duì)到阻塞隊(duì)列,如果任務(wù)執(zhí)行的時(shí)間比較長,沒有釋放,會導(dǎo)致越來越多的任務(wù)堆積到阻塞隊(duì)列,最后導(dǎo)致機(jī)器的內(nèi)存使用不停的飆升,造成JVM OOM。
?
14. 直接大文件或者一次性從數(shù)據(jù)庫讀取太多數(shù)據(jù)到內(nèi)存,可能導(dǎo)致OOM問題
如果一次性把大文件或者數(shù)據(jù)庫太多數(shù)據(jù)達(dá)到內(nèi)存,是會導(dǎo)致OOM的。所以,為什么查詢DB數(shù)據(jù)庫,一般都建議分批。
讀取文件的話,一般問文件不會太大,才使用Files.readAllLines()。為什么呢?因?yàn)樗侵苯影盐募甲x到內(nèi)存的,預(yù)估下不會OOM才使用這個(gè)吧,可以看下它的源碼:
public?static?List?readAllLines(Path?path,?Charset?cs)?throws?IOException?{
????try?(BufferedReader?reader?=?newBufferedReader(path,?cs))?{
????????List?result?=?new?ArrayList<>();
????????for?(;;)?{
????????????String?line?=?reader.readLine();
????????????if?(line?==?null)
????????????????break;
????????????result.add(line);
????????}
????????return?result;
????}
}
如果是太大的文件,可以使用Files.line()按需讀取,當(dāng)時(shí)讀取文件這些,一般是使用完需要「關(guān)閉資源流」的哈
15. 先查詢,再更新/刪除的并發(fā)一致性問題
再日常開發(fā)中,這種代碼實(shí)現(xiàn)經(jīng)常可見:先查詢是否有剩余可用的票,再去更新票余量。
if(selectIsAvailable(ticketId){?
????1、deleteTicketById(ticketId)?
????2、給現(xiàn)金增加操作?
}else{?
????return?“沒有可用現(xiàn)金券”?
}
如果是并發(fā)執(zhí)行,很可能有問題的,應(yīng)該利用數(shù)據(jù)庫的更新/刪除的原子性,正解如下:
if(deleteAvailableTicketById(ticketId)?==?1){?
????1、給現(xiàn)金增加操作?
}else{?
????return?“沒有可用現(xiàn)金券”?
}
16. 數(shù)據(jù)庫使用utf-8存儲, 插入表情異常的坑
低版本的MySQL支持的utf8編碼,最大字符長度為 3 字節(jié),但是呢,存儲表情需要4個(gè)字節(jié),因此如果用utf8存儲表情的話,會報(bào)SQLException: Incorrect string value: '\xF0\x9F\x98\x84' for column,所以一般用utf8mb4編碼去存儲表情。
17. 事務(wù)未生效的坑
日常業(yè)務(wù)開發(fā)中,我們經(jīng)常跟事務(wù)打交道,「事務(wù)失效」主要有以下幾個(gè)場景:
底層數(shù)據(jù)庫引擎不支持事務(wù) 在非public修飾的方法使用 rollbackFor屬性設(shè)置錯(cuò)誤 本類方法直接調(diào)用 異常被try...catch吃了,導(dǎo)致事務(wù)失效。
其中,最容易踩的坑就是后面兩個(gè),「注解的事務(wù)方法給本類方法直接調(diào)用」,偽代碼如下:
public?class?TransactionTest{
??public?void?A(){
????//插入一條數(shù)據(jù)
????//調(diào)用方法B?(本地的類調(diào)用,事務(wù)失效了)
????B();
??}
??
??@Transactional
??public?void?B(){
????//插入數(shù)據(jù)
??}
}
如果異常被catch住,「那事務(wù)也是會失效呢」~,偽代碼如下:
@Transactional
public?void?method(){
??try{
????//插入一條數(shù)據(jù)
????insertA();
????//更改一條數(shù)據(jù)
????updateB();
??}catch(Exception?e){
????logger.error("異常被捕獲了,那你的事務(wù)就失效咯",e);
??}
}
18. 當(dāng)反射遇到方法重載的坑
/**
?*??反射demo
?*??@author?撿田螺的小男孩
?*/
public?class?ReflectionTest?{
????private?void?score(int?score)?{
????????System.out.println("int?grade?="?+?score);
????}
????private?void?score(Integer?score)?{
????????System.out.println("Integer?grade?="?+?score);
????}
????public?static?void?main(String[]?args)?throws?Exception?{
????????ReflectionTest?reflectionTest?=?new?ReflectionTest();
????????reflectionTest.score(100);
????????reflectionTest.score(Integer.valueOf(100));
????????reflectionTest.getClass().getDeclaredMethod("score",?Integer.TYPE).invoke(reflectionTest,?Integer.valueOf("60"));
????????reflectionTest.getClass().getDeclaredMethod("score",?Integer.class).invoke(reflectionTest,?Integer.valueOf("60"));
????}
}
運(yùn)行結(jié)果:
int?grade?=100
Integer?grade?=100
int?grade?=60
Integer?grade?=60
如果「不通過反射」,傳入Integer.valueOf(100),走的是Integer重載。但是呢,反射不是根據(jù)入?yún)㈩愋痛_定方法重載的,而是「以反射獲取方法時(shí)傳入的方法名稱和參數(shù)類型來確定」的
getClass().getDeclaredMethod("score",?Integer.class)
getClass().getDeclaredMethod("score",?Integer.TYPE)
19. mysql 時(shí)間 timestamp的坑
有更新語句的時(shí)候,timestamp可能會自動更新為當(dāng)前時(shí)間,看個(gè)demo
CREATE?TABLE?`t`?(
??`a`?int(11)?DEFAULT?NULL,
??`b`?timestamp??NOT?NULL,
??`c`?timestamp?NOT?NULL?DEFAULT?CURRENT_TIMESTAMP?ON?UPDATE?CURRENT_TIMESTAMP
)?ENGINE=InnoDB?DEFAULT?CHARSET=utf8
我們可以發(fā)現(xiàn) 「c列」 是有CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,所以c列會隨著記錄更新而「更新為當(dāng)前時(shí)間」。但是b列也會隨著有記錄更新為而「更新為當(dāng)前時(shí)間」。

可以使用datetime代替它,需要更新為當(dāng)前時(shí)間,就把now()賦值進(jìn)來,或者修改mysql的這個(gè)參數(shù)explicit_defaults_for_timestamp。
20. mysql8數(shù)據(jù)庫的時(shí)區(qū)坑
之前我們對mysql數(shù)據(jù)庫進(jìn)行升級,新版本為8.0.12。但是升級完之后,發(fā)現(xiàn)now()函數(shù),獲取到的時(shí)間比北京時(shí)間晚8小時(shí),原來是因?yàn)閙ysql8默認(rèn)為美國那邊的時(shí)間,需要指定下時(shí)區(qū)
jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&
serverTimezone=Asia/Shanghai
參考與感謝
Java業(yè)務(wù)開發(fā)常見錯(cuò)誤100例: https://time.geekbang.org/column/article/220230
