多线程交替打印 condition和semaphore

经常被问 多个线程如何交替打印?
下面来两种方式

Rentrantlock 的 condition


class ShareResource {
    private int number = 1;// 控制线程谁开始运行
    private Lock lock =   new ReentrantLock();
    private Condition c1 = lock.newCondition();
    private Condition c2 = lock.newCondition();
    private Condition c3 = lock.newCondition();

    public void print1(){
        lock.lock();
        while(number != 1){
            try {
                c1.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        number = 2;
        System.out.println("线程1打印");
        c2.signal();
        lock.unlock();

    }

    public void print2(){
        lock.lock();
        while(number != 2){
            try {
                c2.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        number = 3;
        System.out.println("线程2打印");
        c3.signal();
        lock.unlock();

    }

    public void print3(){
        lock.lock();
        while(number != 3){
            try {
                c3.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        number = 1;
        System.out.println("线程3打印");
        c1.signal();
        lock.unlock();

    }

}
public class SyncReentrantLockTest {
    public static void main(String[] args) {
        ShareResource shareResource = new ShareResource();
        new Thread(()->{
            for (int i = 0; i < 10 ; i++) {
                shareResource.print1();
            }
        }).start();

        new Thread(()->{
            for (int i = 0; i < 10 ; i++) {
                shareResource.print2();
            }
        }).start();
        new Thread(()->{
            for (int i = 0; i < 10 ; i++) {
                shareResource.print3();
            }
        }).start();
    }
}

结果

线程1打印
线程2打印
线程3打印
线程1打印
线程2打印
线程3打印

Semaphore



public class SemaphoreTest {
    private  Semaphore t1 = new Semaphore(1);
    private Semaphore t2 = new Semaphore(0);
    private void print1(){
        try {
            t1.acquire();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("线程1打印");
        t2.release();
    }

    private void print2(){
        try {
            t2.acquire();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("线程2打印");
        t1.release();

    }

    public static void main(String[] args) {
        SemaphoreTest semaphoreTest = new SemaphoreTest();
        new Thread(()->{
            for (int i = 0; i < 2 ; i++) {
                semaphoreTest.print1();
            }
        }).start();

        new Thread(()->{
            for (int i = 0; i < 2 ; i++) {
                semaphoreTest.print2();
            }
        }).start();


    }
}

结果

线程1打印
线程2打印
线程1打印
线程2打印

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值