一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解Java多線程tryLock()方法使用

詳解Java多線程tryLock()方法使用

2022-03-06 00:44不高興就喝水叭 Java教程

本文主要介紹了Java多線程tryLock()方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

tryLock(long time, TimeUnit unit) 的作用在給定等待時長內鎖沒有被另外的線程持有,并且當前線程也沒有被中斷,則獲得該鎖,通過該方法可以實現鎖對象的限時等待。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.wkcto.lock.reentrant;
 
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
 
/**
 *tryLock(long time, TimeUnit unit) 的基本使用
 */
public class Test07 {
    static class TimeLock implements Runnable{
        private static ReentrantLock lock = new ReentrantLock();    //定義鎖對象
 
        @Override
        public void run() {
            try {
                if ( lock.tryLock(3, TimeUnit.SECONDS) ){       //獲得鎖返回true
                    System.out.println(Thread.currentThread().getName() + "獲得鎖,執行耗時任務");
//                    Thread.sleep(4000);         //假設Thread-0線程先持有鎖,完成任務需要4秒鐘,Thread-1線程嘗試獲得鎖,Thread-1線程在3秒內還沒有獲得鎖的話,Thread-1線程會放棄
                    Thread.sleep(2000);          //假設Thread-0線程先持有鎖,完成任務需要2秒鐘,Thread-1線程嘗試獲得鎖,Thread-1線程會一直嘗試,在它約定嘗試的3秒內可以獲得鎖對象
                }else {         //沒有獲得鎖
                    System.out.println(Thread.currentThread().getName() + "沒有獲得鎖");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                if (lock.isHeldByCurrentThread()){
                    lock.unlock();
                }
            }
        }
    }
 
    public static void main(String[] args) {
        TimeLock timeLock = new TimeLock();
 
        Thread t1 = new Thread(timeLock);
        Thread t2 = new Thread(timeLock);
        t1.start();
        t2.start();
    }
}

tryLock()僅在調用時鎖定未被其他線程持有的鎖,如果調用方法時,鎖對象對其他線程持有,則放棄,調用方法嘗試獲得沒,如果該鎖沒有被其他線程占用則返回true表示鎖定成功; 如果鎖被其他線程占用則返回false,不等待。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.wkcto.lock.reentrant;
 
import java.util.concurrent.locks.ReentrantLock;
 
/**
 *tryLock()
 *  當鎖對象沒有被其他線程持有的情況下才會獲得該鎖定
 */
public class Test08 {
    static class Service{
        private ReentrantLock lock = new ReentrantLock();
        public void serviceMethod(){
            try {
                if (lock.tryLock()){
                    System.out.println(Thread.currentThread().getName() + "獲得鎖定");
                    Thread.sleep(3000);     //模擬執行任務的時長
                }else {
                    System.out.println(Thread.currentThread().getName() + "沒有獲得鎖定");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                if (lock.isHeldByCurrentThread()){
                    lock.unlock();
                }
            }
        }
    }
 
    public static void main(String[] args) throws InterruptedException {
        Service service = new Service();
        Runnable r = new Runnable() {
            @Override
            public void run() {
                service.serviceMethod();
            }
        };
 
        Thread t1 = new Thread(r);
        t1.start();
        Thread.sleep(50);       //睡眠50毫秒,確保t1線程鎖定
        Thread t2 = new Thread(r);
        t2.start();
    }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.wkcto.lock.reentrant;
 
import java.util.Random;
import java.util.concurrent.locks.ReentrantLock;
 
/**
 * 使用tryLock()可以避免死鎖
 */
public class Test09 {
    static class  IntLock implements Runnable{
        private static ReentrantLock lock1 = new ReentrantLock();
        private static ReentrantLock lock2 = new ReentrantLock();
        private int lockNum;        //用于控制鎖的順序
 
        public IntLock(int lockNum) {
            this.lockNum = lockNum;
        }
 
        @Override
        public void run() {
            if ( lockNum % 2 == 0 ){    //偶數先鎖1,再鎖2
                while (true){
                    try {
                        if (lock1.tryLock()){
                            System.out.println(Thread.currentThread().getName() + "獲得鎖1, 還想獲得鎖2");
                            Thread.sleep(new Random().nextInt(100));
 
                            try {
                                if (lock2.tryLock()){
                                    System.out.println(Thread.currentThread().getName() + "同時獲得鎖1與鎖2 ----完成任務了");
                                    return;         //結束run()方法執行,即當前線程結束
                                }
                            } finally {
                                if (lock2.isHeldByCurrentThread()){
                                    lock2.unlock();
                                }
                            }
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        if (lock1.isHeldByCurrentThread()){
                            lock1.unlock();
                        }
                    }
                }
            }else {     //奇數就先鎖2,再鎖1
                while (true){
                    try {
                        if (lock2.tryLock()){
                            System.out.println(Thread.currentThread().getName() + "獲得鎖2, 還想獲得鎖1");
                            Thread.sleep(new Random().nextInt(100));
 
                            try {
                                if (lock1.tryLock()){
                                    System.out.println(Thread.currentThread().getName() + "同時獲得鎖1與鎖2 ----完成任務了");
                                    return;         //結束run()方法執行,即當前線程結束
                                }
                            } finally {
                                if (lock1.isHeldByCurrentThread()){
                                    lock1.unlock();
                                }
                            }
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        if (lock2.isHeldByCurrentThread()){
                            lock2.unlock();
                        }
                    }
                }
            }
        }
    }
    public static void main(String[] args) {
        IntLock intLock1 = new IntLock(11);
        IntLock intLock2 = new IntLock(22);
        Thread t1 = new Thread(intLock1);
        Thread t2 = new Thread(intLock2);
        t1.start();
        t2.start();
        //運行后,使用tryLock()嘗試獲得鎖,不會傻傻的等待,通過循環不停的再次嘗試,如果等待的時間足夠長,線程總是會獲得想要的資源
    }
}

到此這篇關于詳解Java多線程tryLock()方法使用的文章就介紹到這了,更多相關Java tryLock()內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://juejin.cn/post/7023267765918105630

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 私人影院在线播放 | 秘书小说阿蛮 | 久青草国产观看在线视频 | 国产成人精品视频一区二区不卡 | 成年性香蕉漫画在线观看 | 欧美精品综合一区二区三区 | 波多野结衣不卡 | 白丝美女用胸伺候主人 | 521色香蕉网在线观看免费 | 久久99精品涩AV毛片观看 | 欧美日韩人成在线观看 | 国产免费一区二区三区免费视频 | 国产在线99 | 久草在线精彩免费视频 | 大香线一本 | 国产欧美综合一区二区 | 精品精品国产自在香蕉网 | 美女机巴 | 超兴奋朋友的中文字幕下 | 四虎最新永久免费视频 | 男人的j进入女人的j免费 | 国产精品福利在线观看入口 | www.麻豆视频 | 天天爱天天插 | 亚洲六月丁香六月婷婷色伊人 | 午夜小福利 | 成人18视频在线观看 | 我与肥熟老妇的性事 | 亚洲六月丁香六月婷婷色伊人 | 色综合久久久 | 国产激情视频 | 国产一级视频在线观看 | 国产在线观看网站 | 欧美另类videos另类粗暴 | 99在线免费观看 | 欧美一级鲁丝片免费看 | 干操网 | 亚洲国产精品成人午夜在线观看 | 国产日韩欧美视频 | 久久99r66热这里只有精品 | 亚洲天堂影院在线观看 |