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

Java多線程編程概述
Java多線程的安全問題
Java多線程同步
Java多線程間的通信
Java線程Lock
Java多線程管理
保障線程安全的設(shè)計(jì)技術(shù)
Java鎖的優(yōu)化及注意事項(xiàng)
Java多線程集合
【Java多線程】單例模式與多線程

Java多線程:lockInterruptibly()方法

lockInterruptibly() 方法的作用:如果當(dāng)前線程未被中斷則獲得鎖,如果當(dāng)前線程被中斷則出現(xiàn)異常。

package com.wkcto.lock.reentrant;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * lockInterruptibly() 方法的作用:如果當(dāng)前線程未被中斷則獲得鎖,如果當(dāng)前線程被中斷則出現(xiàn)異常.
 */
public class Test05 {
    static class Servier{
        private Lock lock = new ReentrantLock();        //定義鎖對象
        public void serviceMethod(){
            try {
//                lock.lock();        //獲得鎖定,即使調(diào)用了線程的interrupt()方法,也沒有真正的中斷線程
                lock.lockInterruptibly();   //如果線程被中斷了,不會獲得鎖,會產(chǎn)生異常
                System.out.println(Thread.currentThread().getName() + "-- begin lock");
                //執(zhí)行一段耗時的操作
                for (int i = 0; i < Integer.MAX_VALUE; i++) {
                    new StringBuilder();
                }
                System.out.println( Thread.currentThread().getName() + " -- end lock");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                System.out.println( Thread.currentThread().getName() + " ***** 釋放鎖");
                lock.unlock();      //釋放鎖
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Servier  s = new Servier();
        Runnable r = new Runnable() {
            @Override
            public void run() {
                s.serviceMethod();
            }
        };
        Thread t1 = new Thread(r);
        t1.start();

        Thread.sleep(50);
        Thread t2 = new Thread(r);
        t2.start();
        Thread.sleep(50);
        t2.interrupt();     //中斷t2線程
    }
}

對于synchronized內(nèi)部鎖來說,如果一個線程在等待鎖,只有兩個結(jié)果:要么該線程獲得鎖繼續(xù)執(zhí)行;要么就保持等待。

對于ReentrantLock可重入鎖來說,提供另外一種可能,在等待鎖的過程中,程序可以根據(jù)需要取消對鎖的請求。

package com.wkcto.lock.reentrant;

import com.wkcto.pipestream.Test2;

import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 *  通過ReentrantLock鎖的lockInterruptibly()方法避免死鎖的產(chǎn)生
 */
public class Test06 {
    static  class  IntLock implements Runnable{
        //創(chuàng)建兩個ReentrantLock 鎖對象
        public static ReentrantLock lock1 = new ReentrantLock();
        public static ReentrantLock lock2 = new ReentrantLock();
        int lockNum;        //定義整數(shù)變量,決定使用哪個鎖

        public IntLock(int lockNum) {
            this.lockNum = lockNum;
        }

        @Override
        public void run() {
            try {
                if ( lockNum % 2 == 1){         //奇數(shù),先鎖1,再鎖2
                    lock1.lockInterruptibly();
                    System.out.println(Thread.currentThread().getName() + "獲得鎖1,還需要獲得鎖2");
                    Thread.sleep(new Random().nextInt(500));
                    lock2.lockInterruptibly();
                    System.out.println(Thread.currentThread().getName() + "同時獲得了鎖1與鎖2....");
                }else {     //偶數(shù),先鎖2,再鎖1
                    lock2.lockInterruptibly();
                    System.out.println(Thread.currentThread().getName() + "獲得鎖2,還需要獲得鎖1");
                    Thread.sleep(new Random().nextInt(500));
                    lock1.lockInterruptibly();
                    System.out.println(Thread.currentThread().getName() + "同時獲得了鎖1與鎖2....");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                if ( lock1.isHeldByCurrentThread())         //判斷當(dāng)前線程是否持有該鎖
                    lock1.unlock();
                if (lock2.isHeldByCurrentThread())
                    lock2.unlock();
                System.out.println( Thread.currentThread().getName() + "線程退出");
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        IntLock intLock1 = new IntLock(11);
        IntLock intLock2  = new IntLock(22);

        Thread t1 = new Thread(intLock1);
        Thread t2 = new Thread(intLock2);
        t1.start();
        t2.start();

        //在main線程,等待3000秒,如果還有線程沒有結(jié)束就中斷該線程
        Thread.sleep(3000);

        //可以中斷任何一個線程來解決死鎖, t2線程會放棄對鎖1的申請,同時釋放鎖2,  t1線程會完成它的任務(wù)
        if (t2.isAlive()){ t2.interrupt();}
    }
}
全部教程
主站蜘蛛池模板: 九九精品视频一区在线 | 香蕉1024 | 一区二区三区中文国产亚洲 | 55夜色66夜色国产亚洲精品区 | 一个人www在线视频 一二三四在线视频社区8 | 中文字幕日韩精品亚洲七区 | 成年免费大片黄在线观看视频 | 女人牲交视频一级毛片 | 黄大色大片免费久久 | 99在线精品视频在线观看 | 男女在线观看视频 | 国产成人综合亚洲亚洲欧美 | 色www亚洲 | 亚洲女人国产香蕉久久精品 | 欧美日韩一区二区在线观看视频 | 日韩三级中文字幕 | 老司机午夜精品视频播放 | 亚洲人成小说色在线 | 亚洲国产午夜 | 国产成人免费永久播放视频平台 | 欧美一级特黄视频 | 国产精品2020观看久久 | 岛国在线播放v片免费 | 污污网站大全 | av福利导导航 | 成人黄色免费看 | 亚洲综合久久1区2区3区 | 天天摸天天碰天天碰 | 成人在线播放视频 | 九月婷婷人人澡人人添人人爽 | 日韩在线一区二区三区免费视频 | 国产亚洲欧洲国产综合一区 | 麻豆一区二区 | 国产成人综合网亚洲欧美在线 | 一本大道香蕉高清视频在线 | 成人免费黄色 | 久久er| 美女大黄网站 | 香蕉久久综合 | 欧美成人精品第一区首页 | 精品h视频 |