站点图标 度崩网-几度崩溃

鍙噸鍏ラ攣

鎸囧湪鍚屼竴涓嚎绋嬪湪澶栧眰鏂规硶鑾峰彇閿佺殑鏃跺€欙紝杩涘叆鍐呭眰鏂规硶浼氳嚜鍔ㄨ幏鍙栭攣銆?/strong>

涓轰簡閬垮厤姝婚攣鐨勫彂鐢燂紝JDK 涓熀鏈兘鏄彲閲嶅叆閿併€?/p>

涓嬮潰鎴戜滑鏉ユ祴璇曚竴涓?synchronized 鍜?nbsp;java.util.concurrent.lock.ReentrantLock 閿佺殑鍙噸鍏ユ€?/p>

package constxiong.concurrency.a019;/** * 娴嬭瘯 synchronized 鍔犻攣 鍙噸鍏ユ€? * @author ConstXiong * @date 2019-09-20 15:55:27 */public class TestSynchronizedReentrant {public static void main(String[] args) {new Thread(new SynchronizedReentrant()).start();}}class SynchronizedReentrant implements Runnable {private final Object obj = new Object();/** * 鏂规硶1锛岃皟鐢ㄦ柟娉? */public void method1() {synchronized (obj) {System.out.println(Thread.currentThread().getName() + " method1()");method2();}}/** * 鏂规硶2锛屾墦鍗板墠鑾峰彇 obj 閿? * 濡傛灉鍚屼竴绾跨▼锛岄攣涓嶅彲閲嶅叆鐨勮瘽锛宮ethod2 闇€瑕佺瓑寰?method1 閲婃斁 obj 閿? */public void method2() {synchronized (obj) {System.out.println(Thread.currentThread().getName() + " method2()");}}@Overridepublic void run() {//绾跨▼鍚姩 鎵ц鏂规硶1method1();}}

鎵撳嵃缁撴灉锛?/p>

Thread-0 method1()Thread-0 method2()
package constxiong.concurrency.a019;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;/** * 娴嬭瘯 ReentrantLock 鐨勫彲閲嶅叆鎬? * @author ConstXiong * @date 2019-09-20 16:24:52 */public class TestLockReentrant {public static void main(String[] args) {new Thread(new LockReentrant()).start();}}class LockReentrant implements Runnable {private final Lock lock = new ReentrantLock();/** * 鏂规硶1锛岃皟鐢ㄦ柟娉? */public void method1() {lock.lock();try {System.out.println(Thread.currentThread().getName() + " method1()");method2();} finally {lock.unlock();}}/** * 鏂规硶2锛屾墦鍗板墠鑾峰彇 obj 閿? * 濡傛灉鍚屼竴绾跨▼锛岄攣涓嶅彲閲嶅叆鐨勮瘽锛宮ethod2 闇€瑕佺瓑寰?method1 閲婃斁 obj 閿? */public void method2() {lock.lock();try {System.out.println(Thread.currentThread().getName() + " method2()");} finally {lock.unlock();}}@Overridepublic void run() {//绾跨▼鍚姩 鎵ц鏂规硶1method1();}}

鎵撳嵃缁撴灉锛?/p>

Thread-0 method1()Thread-0 method2()

娴嬭瘯涓嶅彲閲嶅叆閿?/strong>

鎴戝湪 JDK 涓病鎵惧埌鍙噸鍏ラ攣锛屾墍浠ヨ€冭檻鑷繁瀹炵幇涓€涓嬨€備袱绉嶆柟寮忥細閫氳繃 synchronized wait notify 瀹炵幇锛涢€氳繃 CAS + 鑷棆鏂瑰紡瀹炵幇

1锛?synchronized wait notify 鏂瑰紡瀹炵幇

package constxiong.concurrency.a019;/** * 涓嶅彲閲嶅叆閿侊紝閫氳繃 synchronized wait notify 瀹炵幇 * @author ConstXiong * @date 2019-09-20 16:53:34 */public class NonReentrantLockByWait {//鏄惁琚攣private volatile boolean locked = false;//鍔犻攣public synchronized void lock() {//褰撴煇涓嚎绋嬭幏鍙栭攣鎴愬姛锛屽叾浠栫嚎绋嬭繘鍏ョ瓑寰呯姸鎬?while (locked) {try {wait();} catch (InterruptedException e) {e.printStackTrace();}}//鍔犻攣鎴愬姛锛宭ocked 璁剧疆涓?truelocked = true;}//閲婃斁閿?public synchronized void unlock() {locked = false;notify();}}

2锛?nbsp;閫氳繃 CAS + 鑷棆 鏂瑰紡瀹炵幇

package constxiong.concurrency.a019;import java.util.concurrent.atomic.AtomicReference;/** * 涓嶅彲閲嶅叆閿侊紝閫氳繃 CAS + 鑷棆 瀹炵幇 * @author ConstXiong * @date 2019-09-20 16:53:34 */public class NonReentrantLockByCAS {private AtomicReference<Thread> lockedThread = new AtomicReference<Thread>();public void lock() {Thread t = Thread.currentThread();//褰?lockedThread 鎸佹湁寮曠敤鍙橀噺涓?null 鏃讹紝璁剧疆 lockedThread 鎸佹湁寮曠敤涓?褰撳墠绾跨▼鍙橀噺while (!lockedThread.compareAndSet(null, t)) {//鑷棆锛岀┖寰幆锛岀瓑鍒伴攣琚噴鏀?}}public void unlock() {//濡傛灉鏄湰绾跨▼閿佸畾鐨勶紝鍙互鎴愬姛閲婃斁閿?lockedThread.compareAndSet(Thread.currentThread(), null);}}

娴嬭瘯绫?/p>

package constxiong.concurrency.a019;/** * 娴嬭瘯涓嶅彲閲嶅叆閿? * @author ConstXiong * @date 2019-09-20 18:08:55 */public class TestLockNonReentrant{public static void main(String[] args) {new Thread(new LockNonReentrant()).start();}}class LockNonReentrant implements Runnable {//private final NonReentrantLockByWait lock = new NonReentrantLockByWait();private final NonReentrantLockByCAS lock = new NonReentrantLockByCAS();/** * 鏂规硶1锛岃皟鐢ㄦ柟娉? */public void method1() {lock.lock();try {System.out.println(Thread.currentThread().getName() + " method1()");method2();} finally {lock.unlock();}}/** * 鏂规硶2锛屾墦鍗板墠鑾峰彇 obj 閿? * 濡傛灉鍚屼竴绾跨▼锛岄攣涓嶅彲閲嶅叆鐨勮瘽锛宮ethod2 闇€瑕佺瓑寰?method1 閲婃斁 obj 閿? */public void method2() {lock.lock();try {System.out.println(Thread.currentThread().getName() + " method2()");} finally {lock.unlock();}}@Overridepublic void run() {//绾跨▼鍚姩 鎵ц鏂规硶1method1();}}

娴嬭瘯缁撴灉锛岄兘鏄湪 method1锛岃皟鐢?method2 鐨勬椂鍊欙紝瀵艰嚧浜嗘閿侊紝绾跨▼涓€鐩寸瓑寰呮垨鑰呰嚜鏃嬩笅鍘汇€?/p>

鍙傝€冿細