鍙噸鍏ラ攣

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

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

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

  • 娴嬭瘯 synchronized 鍔犻攣 鍙噸鍏ユ€?/li>
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()
  • 娴嬭瘯 ReentrantLock 鐨勫彲閲嶅叆鎬?/li>
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>

-度崩网-几度崩溃

鍙傝€冿細

给TA打赏
共{{data.count}}人
人已打赏
Java

sleep()和wait()有什么区别?

2020-7-31 0:45:00

Java

Runnable和Callable有什么区别?

2020-7-31 0:48:20

本站所发布的一切源码、模板、应用等文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版,购买注册,得到更好的正版服务。如有侵权。本站内容适用于DMCA政策。若您的权利被侵害,请与我们联系处理,站长 QQ: 84087680 或 点击右侧 私信:盾给网 反馈,我们将尽快处理。
⚠️
本站所发布的一切源码、模板、应用等文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版,购买注册,得到更好的正版服务。如有侵权。本站内容适用于DMCA政策
若您的权利被侵害,请与我们联系处理,站长 QQ: 84087680 或 点击右侧 私信:盾给网 反馈,我们将尽快处理。
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索