???????????????е?????????????? ??????θ??????? ????輸????????????! ?????????д???й??????????е?Blog
????1. synchonrize??θ????????
?????????????? ????????????????????:
????· ?????????
????· synchonrize???????÷??????
????· synchonrize??volatile???
????· synchonrize??juc?е??????
????· ???????????в???????????
????1.1 ?????????
?????????????????????????????????? ?????д?ò????????.

???????????? ??????????? sleep?? yield?? wait?? notify?? notifyAll???????????? ???????????. ?????????:

????wait?г???Object???????壬 ?????????? ?????????????? ?????????synchronized??????????????wait. notify???????? Object.wait()??notify()??????????????壬 ?????????synchronized????????.
????yield()????????????2???: ????????????????????????????????????? ???У? ??? CPU ???????????????? ???????????????????. ????yield()???????“????”?? ???????л?????????????????????????.
????1.2 synchonrize??????
????synchronize??????????????5???÷?
??????????????????? ???(1)instance method/(2)static method?? ????????????????
????????????????????? ???(3)synchronize(this)?? (4)synchonrize(XXX.class)?? (5)synchonrize(mutex)
public class SyncMethod {
private int value = 0;
private final Object mutex = new Object();
public synchronized int incAndGet0() {
return ++value;
}
public int incAndGet1() {
synchronized(this){
return ++value;
}
}
public int incAndGet2() {
synchronized(SyncMethod.class){
return ++value;
}
}
public int incAndGet3() {
synchronized(mutex){
return ++value;
}
}
public static synchonrize int incAndGet4() {
synchronized(mutex){
return ++value;
}
}
}
??????????????:
??????????η??????????????? synchronized???η???????????????????÷????????? ???ξ????????????????????????????е??????.
????synchronized(X.class) ???????????monitor. ????????????????????????????.
????synchronized(this)??synchronized(mutex) ??????????? ??????????????????????????????????????.
????sychronized????????????ò???仯????????類????final???????????????????? ???synchronized?????????????? ??????????????????????λ???? ????????仯?? ?????????????????
????1.3 synchronized??volatile???
???????????synchronized???????????????????????? volatile??????????? ????????????????????????? ???????volatile
???????????д??????????????????????? (++i/i++??????????)?? ???????????е???????????
??????????????????????????????????????????
?????????????????????
????1.4 synchonrize??juc?е??????
????ReentrantLock??????????????synchronize????? ?????????????????? ????????????????. ??????Щ ?????? ??????? ???ж????????????? ?????????ù?????? ??????÷????????? ????????ReetrantLock.
?????????? ???????????????????????synchronized?? ????????????????? ??????????????????????? ??????ReentrantLock??????. ??????JVM???????????????????synchronize??? ?????????????synchronize
????1.5 ???????????в???????????
??????????? ?????????в???????
????Map syncMap = Collections.synchronizedMap(new HashMap());
????if(!map.containsKey("a")){
????map.put("a"??value);
????}
???????Map?????е?????????synchronize??????? ??????????????? ??????? ???????
???????????: ???????е?????????????????????
??????????????????μ??ЩDonts
????Don’t synchronize on an object you’re changing
????Don’t synchronize on a String literal
????Don’t synchronize on auto-boxed values
????Don’t synchronize on null
????Don’t synchronize on a Lock object
????Don’t synchronize on getClass()
????Be careful locking on a thread-safe object with encapsulated locking
????2. Juc?е??????????
????2.1 Semaphore ????????????????????. ???????????????????????????????????????????????.
????2.2 CountDownLatch ????????? ?????????????????. ???????????????????????????????е????????????????????????????.
????2.3 CyclicBarrier ??????????·????? ???Щ????????????????. ??????????????????? ????????????????????? (common barrier point). ???漰???????С??????????У? ??Щ???????????????? ??? CyclicBarrier ??????.????? barrier??????????????????? ?????????????barrier.
????2.4 Phaser ????????????????? ????????????CyclicBarrier??CountDownLatch?? ????????????. ???????????????????????Э?????μ???????Fork/Join????е?????????????????? ???????Phaser??
????2.5 Exchanger ?????????????????????????? ???Щ??????????????. Exchanger???????????? ?????????? ????????????????. ?????????exchange()?????????????????????????? ????????????????????????????. ????????????Exchanger????????? ?????????????????????????????. Exchanger????????? SynchronousQueue ?????????? ????????????????????????б??????.