互斥锁

互斥锁是JDK1.5的新特性,它可以实现和synchronize一样的作用,但更加强大。

public class Demo3_ReentrantLock {

    public static void main(String[] args) {
        Printer3 p = new Printer3();
        new Thread(){
            public void run() {
                while(true){
                    try {
                        p.print1();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        new Thread(){
            public void run() {
                while(true){
                    try {
                        p.print2();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        new Thread(){
            public void run() {
                while(true){
                    try {
                        p.print3();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

}


class Printer3{
    private ReentrantLock r = new ReentrantLock();
    private Condition c1 = r.newCondition();
    private Condition c2 = r.newCondition();
    private Condition c3 = r.newCondition();
    private int flag = 1;
    public void print1() throws InterruptedException{
        r.lock();      // 获取锁         
        if(flag != 1){
            c1.await();   // 当前线程等待
        }
        System.out.print("互");
        System.out.print("斥");
        System.out.print("锁");
        System.out.println();
        flag = 2;
        c2.signal();    // 唤醒c2
    }

    public void print2() throws InterruptedException{
        r.lock();                        
        if(flag != 2){
            c2.await();
        }
        System.out.print("r");
        System.out.print("e");
        System.out.print("e");
        System.out.print("n");
        System.out.print("t");
        System.out.print("r");
        System.out.print("a");
        System.out.print("n");
        System.out.print("t");
        System.out.println();
        flag = 3;
        c3.signal();
    }

    public void print3() throws InterruptedException{
        r.lock();                         
        if(flag != 3){
            c3.await();
        }
        System.out.print("L");
        System.out.print("o");
        System.out.print("c");
        System.out.print("k");
        System.out.println();
        flag = 1;
        c1.signal();
    }
}

虽然用synchronize也可以实现线程之间的通信(即让线程按照希望的顺序执行),但是用ReentrantLock跟简单,可以唤醒指定线程,而用synchronize需要将所有线程唤醒在逐个进行判断,上面这段代码实现的是交替输出:
这里写图片描述

也可以用synchronize实现:

public class Demo2_NotifyAll {

    public static void main(String[] args) {
        Printer2 p = new Printer2();
        new Thread () {
            public void run(){
                while(true){
                    try {
                        p.print1();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        new Thread () {
            public void run(){
                while(true){
                    try {
                        p.print2();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        new Thread () {
            public void run(){
                while(true){
                    try {
                        p.print3();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

}

class Printer2{
    private int flag = 1;
    public void print1() throws InterruptedException{
        synchronized(this) {                
            while(flag != 1){
                this.wait();
            }
            System.out.print("互");
            System.out.print("斥");
            System.out.print("锁");
            System.out.println();
            flag = 2;
            this.notifyAll();   // 唤醒所有等待线程
        }
    }

    public void print2() throws InterruptedException{
        synchronized(this){                         
            while(flag != 2){
                this.wait();
            }
            System.out.print("r");
            System.out.print("e");
            System.out.print("e");
            System.out.print("n");
            System.out.print("t");
            System.out.print("r");
            System.out.print("a");
            System.out.print("n");
            System.out.print("t");
            System.out.println();
            flag = 3;
            this.notifyAll();
        }
    }

    public void print3() throws InterruptedException{
        synchronized(this){                         
            while(flag != 3){
                this.wait();
            }
            System.out.print("L");
            System.out.print("o");
            System.out.print("c");
            System.out.print("k");
            System.out.println();
            flag = 1;
            this.notifyAll();
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值