黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

Java多線程編程概述
Java多線程的安全問題
Java多線程同步
Java多線程間的通信
Java線程Lock
Java多線程管理
保障線程安全的設計技術
Java鎖的優化及注意事項
Java多線程集合
【Java多線程】單例模式與多線程

Java多線程:tryLock()方法

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

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,不等待。

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();
    }
}
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()嘗試獲得鎖,不會傻傻的等待,通過循環不停的再次嘗試,如果等待的時間足夠長,線程總是會獲得想要的資源
    }
}

 

全部教程
主站蜘蛛池模板: 国产又爽又黄又舒服又刺激视频 | 欧美黑人巨大xxxxxxxx | 伊人网色| 欧美午夜影视 | 亚洲美女操| 美女天天操 | 又爽又黄又无遮挡网站 | 五月综合激情 | 午夜剧| 涩涩视频www在线观看入口 | 成人欧美日韩视频一区 | 色综合中文网 | 欧美专区在线播放 | 在线亚洲免费 | 日本免费一区二区三区看片 | 不卡视频在线播放 | 日本高清www无色夜在 | 草逼视频免费观看 | 亚洲视频在线看 | 在线视频你懂得 | 亚洲欧美专区精品久久 | 男女视频在线看 | 免费看一级黄色毛片 | 中文字幕手机在线播放 | 又爽又黄又无遮挡网站 | 一级特级欧美午夜片免费观看 | 日韩欧美一区二区不卡 | 亚洲国产第一 | 黄色免费在线观看视频 | a天堂中文在线官网 | 色偷偷尼玛图亚洲综合 | 日韩欧美国产成人 | 麻豆一区二区三区四区 | 香蕉视频黄色 | 九九热视频在线免费观看 | 日本免费黄色网址 | 国产91精品久久久久999 | 国产亚洲一区二区精品张柏芝 | 国产在线观看成人 | 黄色免费网站在线观看 | 日韩日批|