Java多线程-线程间通信Demo

三个及以上线程间通信(以三个为例)

法① : 利用jdk1.5新特性(互斥锁)

package com.demo.reentranlock;

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

/**
 * 
 * <p>
 * Title: ReentranLockDemo
 * </p>
 * <p>
 * Description: 循环打印step1、step2、step3
 * </p>
 * <p>
 * Company: buaa
 * </p>
 *
 * @author 夜色
 * @date 2016年11月28日
 */
public class ReentranLockDemo {

    public static void main(String[] args) {
        Printer p = new Printer();

        // 通过匿名内部类开启三个线程,分别不断打印step1、step2、step3
        new Thread() {
            public void run() {
                try {
                    while (true) {
                        p.print1();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();

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

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

    }

}

class Printer {

    private ReentrantLock rLock = new ReentrantLock(); // 互斥锁
    private Condition c1 = rLock.newCondition();
    private Condition c2 = rLock.newCondition();
    private Condition c3 = rLock.newCondition();
    private int flag = 1;

    public void print1() throws InterruptedException {
        rLock.lock(); // 上锁

        if (flag != 1) // 条件不成立,等待并释放锁;条件成立则继续
            c1.await();

        System.out.println("step 1"); // 自定义的操作

        flag = 2; // 唤醒print2
        c2.signal();

        rLock.unlock(); // 解锁
    }

    public void print2() throws InterruptedException {
        rLock.lock(); // 上锁

        if (flag != 2) // 条件不成立,等待并释放锁;条件成立则继续
            c2.await();

        System.out.println("step 2"); // 自定义的操作

        flag = 3; // 唤醒print3
        c3.signal();

        rLock.unlock(); // 解锁
    }

    public void print3() throws InterruptedException {
        rLock.lock(); // 上锁

        if (flag != 3) // 条件不成立,等待并释放锁;条件成立则继续
            c3.await();

        System.out.println("step 3"); // 自定义的操作

        flag = 1; // 唤醒print1
        c1.signal();

        rLock.unlock(); // 解锁
    }

}

法②: 利用notifyAll、wait(jdk1.5之前的解决策略)

package com.demo.notifyall;

/**
 * 
 * <p>
 * Title: ReentranLockDemo
 * </p>
 * <p>
 * Description: 循环打印step1、step2、step3
 * </p>
 * <p>
 * Company: buaa
 * </p>
 *
 * @author 夜色
 * @date 2016年11月28日
 */
public class NotifyAllDemo {

    public static void main(String[] args) {
        Printer p = new Printer();

        // 通过匿名内部类开启三个线程,分别不断打印step1、step2、step3
        new Thread() {
            public void run() {
                try {
                    while (true) {
                        p.print1();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();

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

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

    }

}

class Printer {

    private int flag = 1;

    public void print1() throws InterruptedException {
        synchronized (this) { // 上锁
            while (flag != 1) { // 条件不成立,等待并释放锁;条件成立则继续
                this.wait();
            }

            System.out.println("step 1"); // 自定义的操作

            flag = 2;

            this.notifyAll(); // 解锁,并唤醒其他所有等待线程
        }
    }

    public void print2() throws InterruptedException {
        synchronized (this) { // 上锁
            while (flag != 2) { // 条件不成立,等待并释放锁;条件成立则继续
                this.wait();
            }

            System.out.println("step 2"); // 自定义的操作

            flag = 3;

            this.notifyAll(); // 解锁,并唤醒其他所有等待线程
        }
    }

    public void print3() throws InterruptedException {
        synchronized (this) { // 上锁
            while (flag != 3) { // 条件不成立,等待并释放锁;条件成立则继续
                this.wait();
            }

            System.out.println("step 3"); // 自定义的操作

            flag = 1;

            this.notifyAll(); // 解锁,并唤醒其他所有等待线程
        }
    }

}

对比两种方式,体会jdk1.5在进程通信上的改进

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值