慕課網(wǎng) Jimin老師 Java并發(fā)編程入門與高并發(fā)面試 學(xué)習(xí)筆記
Java并發(fā)編程入門與高并發(fā)面試
不可變對(duì)象需要滿足的條件
◆對(duì)象創(chuàng)建以后其狀態(tài)就不能修改
◆對(duì)象所有域都是final類型
◆對(duì)象是正確創(chuàng)建的(在對(duì)象創(chuàng)建期間, this引用沒(méi)有逸出)
方式(可參考String類):
- 將類申明為final
- 將所有的成員申明為私有
- 對(duì)變量不提供set方法枢里,將所有可變成員聲明為final账锹,這樣只能對(duì)其賦值一次
- 通過(guò)構(gòu)造器初始化所有成員雁比,進(jìn)行深度拷貝
- 在get方法中,不返回對(duì)象的本身侧蘸,而是返回其對(duì)象的拷貝
final關(guān)鍵字:類裁眯、 方法、變量
◆修飾類:不能被繼承
◆修飾方法: 1闺魏、鎖定方法不被繼承類修改; 2、效率
◆修飾變量:基本數(shù)據(jù)類型變量俯画、引用類型變量
package com.huhao.concurrency.example.immutable;
import com.google.common.collect.Maps;
import com.huhao.concurrency.annoations.NotThreadSafe;
import lombok.extern.slf4j.Slf4j;
import java.util.Map;
@Slf4j
@NotThreadSafe
public class ImmutableExample1 {
private final static Integer a = 1;
private final static String b = "2";
private final static Map<Integer, Integer> map = Maps.newHashMap();
static {
map.put(1, 2);
map.put(3, 4);
map.put(5, 6);
}
public static void main(String[] args) {
// a = 2;
// b = "3";
// map = Maps.newHashMap();不允許聲明成新的變量析桥,
//但是可以修改,新增內(nèi)部的值
map.put(1, 3);
log.info("{}", map.get(1));
log.info("{}", map);
}
private void test(final int a) {
//不可以改
//a = 1;
}
}
除了final可設(shè)置不可變變量,還可以通過(guò)以下類
◆Collections.unmodifiableXXX : Collection. List. Set. Map...
◆Guava : ImmutableXXX : Collection艰垂、List, Set泡仗、 Map...
package com.huhao.concurrency.example.immutable;
import com.google.common.collect.Maps;
import com.huhao.concurrency.annoations.NotThreadSafe;
import com.huhao.concurrency.annoations.ThreadSafe;
import lombok.extern.slf4j.Slf4j;
import java.util.Collections;
import java.util.Map;
/**
* unmodifiableMap
* 不可修改
* 修改時(shí)不會(huì)編譯報(bào)錯(cuò),但是運(yùn)行會(huì)報(bào)錯(cuò)拋出異常
*/
@Slf4j
@ThreadSafe
public class ImmutableUnmodifiableMap {
private static Map<Integer, Integer> map = Maps.newHashMap();
static {
map.put(1, 2);
map.put(3, 4);
map.put(5, 6);
//不可被修改
map = Collections.unmodifiableMap(map);
}
public static void main(String[] args) {
//unmodifiableMap后猜憎,修改會(huì)報(bào)錯(cuò)
map.put(1, 3);
log.info("{}", map.get(1));
log.info("{}", map);
}
}
package com.huhao.concurrency.example.immutable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.huhao.concurrency.annoations.ThreadSafe;
import lombok.extern.slf4j.Slf4j;
/**
* ImmutableImmutable
*/
@Slf4j
@ThreadSafe
public class ImmutableImmutable {
private final static ImmutableList<Integer> list = ImmutableList.of(1, 2, 3);
private final static ImmutableSet set = ImmutableSet.copyOf(list);
private final static ImmutableMap<Integer, Integer>
map = ImmutableMap.of(1, 2, 3, 4);
private final static ImmutableMap<Integer, Integer>
map2 = ImmutableMap.<Integer, Integer>builder()
.put(1, 2).put(3, 4).put(5, 6)
.build();
public static void main(String[] args) {
//會(huì)拋出異常
//list.add(4);
//會(huì)拋出異常
// map.put(1, 4);
// map2.put(1, 4);
}
}
線程封閉
1娩怎、Ad-hoc線程封閉:程序控制實(shí)現(xiàn),最糟糕,忽略
2胰柑、堆棧封閉:局部變量截亦,無(wú)并發(fā)問(wèn)題,能用局部變量就不用全局變量
3柬讨、ThreadLocal 線程封閉:特別好的封閉方法
eg:動(dòng)態(tài)數(shù)據(jù)源切換時(shí)候崩瓤, 每個(gè)線程請(qǐng)求取到的context_holder都是獨(dú)立的
image.png
線程不安全類與寫法
StringBuilder ->StringBuffer
builder是不安全的,buffer是安全的踩官。不過(guò)在方法里的私有的却桶,用builder效率更高
SimpleDateFormat -> JodaTime
SimpleDateFormat 不安全、每次都是要new SimpleDateFormat (),不然會(huì)報(bào)錯(cuò)颖系。JodaTime是線程安全 的嗅剖,推薦使用,而且還有其他優(yōu)勢(shì)嘁扼。
ArrayList信粮,HashSet,HashMap等Collections
同步容器
◆ArrayList -> Vector, Stack
◆HashMap -> HashTable (key偷拔、value不能為null)
◆Collections.synchronizedXXX (List蒋院、Set、 Map)
并發(fā)容器J.U.C
◆ArrayList -> CopyOnWriteArrayList
HashSet莲绰、TreeSet -> CopyOnWriteArraySet
ConcurrentSkipListSet
◆HashMap欺旧、 TreeMap -> ConcurrentHashMap
ConcurrentSkipListMap