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

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

Java多線程死鎖

線程活性問題是由資源稀缺或者程序自身設計缺陷導致線程一直處于非RUNNABLE狀態,或者線程雖然處于RUNNABLE狀態,但是要執行的任務一直無法進展。

死鎖

如果兩個或者更多的線程因相互等待而被永遠暫停,我們就稱這些線程產生了死鎖。

有關死鎖的一個經典問題是哲學家就餐問題。

package com.wkcto.threadactivity.deadlock;

/**
 * 定義筷子類
 */
public class Chopstick {
    public final  int id;   //筷子編號

    public Chopstick(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Chopstick-" + id;
    }
}
package com.wkcto.threadactivity.deadlock;

/**
 * 模擬哲學家就餐
 */
public class Test {
    public static void main(String[] args) {
        int num = 5;        //有5個哲學家,5根筷子
        //創建一個存儲5根筷子的數組
        Chopstick[] chopsticks = new Chopstick[num];
        //給數組中的筷子賦值
        for (int i = 0; i < chopsticks.length; i++) {
            chopsticks[i] = new Chopstick(i);
        }

        //創建5個哲學家線程
        for (int i = 0; i < num; i++) {
            Philosopher philosopher = new Philosopher(i, chopsticks[i], chopsticks[ (i+1) % num]);
            philosopher.start();
        }
    }
}
package com.wkcto.threadactivity.deadlock;

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

/**
 * 定義哲學家類,繼承Thread線程類
 * 使用顯示鎖ReentrantLock的tryLock()方法申請鎖, 允許在申請鎖時指定一個超時時間
 */
public class Philosopher extends Thread {
    public final int id;    //哲學家編號
    public final Chopstick left;    //左邊筷子
    public final Chopstick right;   //右邊筷子

    public Philosopher(int id, Chopstick left, Chopstick right) {
        super("Philosopher==" + id);
        this.id = id;
        this.left = left;
        this.right = right;

    }

    //哲學家不斷的思考與吃飯
    @Override
    public void run() {
        while (true) {
            think();
            eat();
        }
    }

    final ReentrantLock leftLock = new ReentrantLock();
    final ReentrantLock rightLock = new ReentrantLock();

    private void eat() {
        //吃飯需要先拿左邊筷子,這根筷子就是被當前哲學家獨占使用
        try {
            if (leftLock.tryLock(100, TimeUnit.MILLISECONDS)){
                System.out.println(this + " 拿起左邊筷子");
                Thread.sleep(new Random().nextInt(100));
                if (rightLock.tryLock(100, TimeUnit.MILLISECONDS)){
                    System.out.println(this + " 拿起右邊筷子, 有了一雙筷子,可以吃飯了");
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            if (rightLock.isHeldByCurrentThread()){
                rightLock.unlock();
            }
            if (leftLock.isHeldByCurrentThread()){
                leftLock.unlock();
            }
        }
    }

    private void think() {
        System.out.println(this + " 哲學家正在思考.....");
        try {
            Thread.sleep(new Random().nextInt(100));   //模擬思考時長
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String toString() {
        return "Philosopher{" +
                "id=" + id +
                ", left=" + left +
                ", right=" + right +
                '}';
    }
}
package com.wkcto.threadactivity.deadlock;

import java.util.Random;

/**
 * 定義哲學家類,繼承Thread線程類
 */
public class Philosopher1 extends  Thread{
    public final int id;    //哲學家編號
    public final Chopstick left;    //左邊筷子
    public final Chopstick right;   //右邊筷子

    public Philosopher1(int id, Chopstick left, Chopstick right) {
        super("Philosopher==" + id );
        this.id = id;
        this.left = left;
        this.right = right;
    }

    //哲學家不斷的思考與吃飯
    @Override
    public void run() {
        while (true){
            think();
            eat();
        }
    }

    private void eat() {
        //吃飯需要先拿左邊筷子,這根筷子就是被當前哲學家獨占使用
        synchronized ( left ){
            System.out.println(this + " 拿起左邊的筷子");
            try {
                Thread.sleep(new Random().nextInt(100));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //有了左邊筷子后,還需要拿右邊筷子
            synchronized (right){
                System.out.println(this + "有了一雙筷子,可以吃飯");
            }
        }

    }

    private void think() {
        System.out.println( this + " 哲學家正在思考.....");
        try {
            Thread.sleep(new Random().nextInt(100));   //模擬思考時長
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String toString() {
        return "Philosopher{" +
                "id=" + id +
                ", left=" + left +
                ", right=" + right +
                '}';
    }
}
package com.wkcto.threadactivity.deadlock;

import java.util.Random;

/**
 * 定義哲學家類,繼承Thread線程類
 *  可以使用粗鎖化解決死鎖問題
 *  某一時刻只允許一個哲學家吃飯,在某個哲學家吃飯時,其他哲學家要么進行思考,要么等待
 *  實際上,哲學家吃飯只需要兩根筷子, 現在有5根筷子,可以允許兩個哲學家同時吃飯. 現在使用粗鎖化只允許有一個哲學家吃飯,出現了資源浪費的情況
 */
public class Philosopher2 extends  Thread{
    public final int id;    //哲學家編號
    public final Chopstick left;    //左邊筷子
    public final Chopstick right;   //右邊筷子

    public Philosopher2(int id, Chopstick left, Chopstick right) {
        super("Philosopher==" + id );
        this.id = id;
        this.left = left;
        this.right = right;
    }

    //哲學家不斷的思考與吃飯
    @Override
    public void run() {
        while (true){
            think();
            eat();
        }
    }
    private static  final Object OBJ = new Object();        //定義常量作為鎖對象
    private void eat() {
        synchronized ( OBJ ){
            System.out.println(this + " 拿起左邊的筷子");
            try {
                Thread.sleep(new Random().nextInt(100));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println(this + "拿起右邊筷子后,就有了一雙筷子,可以吃飯");

        }

    }

    private void think() {
        System.out.println( this + " 哲學家正在思考.....");
        try {
            Thread.sleep(new Random().nextInt(100));   //模擬思考時長
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String toString() {
        return "Philosopher{" +
                "id=" + id +
                ", left=" + left +
                ", right=" + right +
                '}';
    }
}
package com.wkcto.threadactivity.deadlock;

import java.util.Random;

/**
 * 定義哲學家類,繼承Thread線程類
 *  可以保證所有線程使用相同的鎖的順序來避免死鎖
 *  可以給所有的筷子設置一個編號, 對于哲學家來說 ,始終先拿編號小的筷子,再拿編號大的筷子
 */
public class Philosopher3 extends  Thread{
    public final int id;    //哲學家編號
    public final Chopstick left;    //左邊筷子
    public final Chopstick right;   //右邊筷子

    public Philosopher3(int id, Chopstick left, Chopstick right) {
        super("Philosopher==" + id );
        this.id = id;
       //根據筷子編號賦值,如果left筷子編號小于right編號正常賦值
        if (left.id < right.id){
            this.left = left;
            this.right = right;
        }else {
            this.left = right;
            this.right = left;
        }
    }

    //哲學家不斷的思考與吃飯
    @Override
    public void run() {
        while (true){
            think();
            eat();
        }
    }
    private void eat() {
        //吃飯需要先拿左邊筷子,這根筷子就是被當前哲學家獨占使用
        synchronized ( left ){
            System.out.println(this + " 拿起左邊的筷子");
            try {
                Thread.sleep(new Random().nextInt(100));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //有了左邊筷子后,還需要拿右邊筷子
            synchronized (right){
                System.out.println(this + "有了一雙筷子,可以吃飯");
            }
        }

    }

    private void think() {
        System.out.println( this + " 哲學家正在思考.....");
        try {
            Thread.sleep(new Random().nextInt(100));   //模擬思考時長
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String toString() {
        return "Philosopher{" +
                "id=" + id +
                ", left=" + left +
                ", right=" + right +
                '}';
    }
}

 

全部教程
主站蜘蛛池模板: 91桃色观看入口 | 久久影视一区 | 亚洲视频免费 | 性xxxx18免费观看 | 丝袜老师在办公室被狠狠 | 久热re这里只有精品视频 | 亚洲欧美人妖另类激情综合区 | 波多野结衣不卡 | 欧美日本高清一本二本三本 | 欧美高清成人videosex | 日日射日日干 | 蜜臀91精品国产免费观看 | 亚洲九九夜夜 | 国产精品成人h视频 | 国产精品久久久久久久午夜片 | 波多野结衣中文字幕在线 | 黄色综合网 | 亚洲精品高清国产一久久 | 在线观看免费黄色网址 | 一级一级一级毛片 | 中文字幕在线观看免费视频 | 日韩毛片高清在线看 | 欧美a视频在线观看 | 黑人黄色一级片 | 五月香福利| 天天色色网 | 亚洲va乱码一区二区三区 | 亚洲欧美日韩中文无线码 | 国产日韩欧美成人 | 国产精品亚欧美一区二区三区 | 98bb国产精品视频 | 亚洲成人伦理 | 亚洲综合第一页 | 深夜福利网站在线 | 2021日日摸夜夜添夜夜添影院 | 婷婷激情网站 | 久久久精彩视频 | 青草草在线观看 | 人人爱人人插 | 狠狠五月婷婷 | 在线观看免费国产成人软件 |