线程通信--

三个线程实现顺序打印0aA1bB

//1、 park--unpark
public class Main {
    static Thread threadA,threada,threadN;
    public static void main(String[] args) {
        Print2 print = new Print2();
        threadA = new Thread(()->{
            for(int i=0;i<26;i++){
                LockSupport.park();
                print.printCharA();
                LockSupport.unpark(threadN);
            }
        });
        threadN = new Thread(()->{
            for(int i=0;i<26;i++){
                print.printNum();
                LockSupport.unpark(threada);
                LockSupport.park();
            }
        });
        threada = new Thread(()->{
            for(int i=0;i<26;i++){
                LockSupport.park();
                print.printChara();
                LockSupport.unpark(threadA);
            }
        });
        threadA.start();
        threadN.start();
        threada.start();
    }
}



public class Main {
    public static void main(String[] args) {
        Print print = new Print();
        new Thread(print::printChara).start();
        new Thread(print::printNum).start();
        new Thread(print::printCharA).start();
        System.err.print("\n");
    }
}

// 2、synchronized加锁
class Print{
    private int flag=1;
    private int count=0;
    public synchronized void printNum(){
        for(int i=0;i<26;i++){
            while (flag!=1){
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            flag=2;
            System.err.print(count);
            notifyAll();
        }
    }
    public synchronized void printChara(){
        for(int i=0;i<26;i++){
            while (flag!=2){
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            flag=3;
            System.err.print((char)(count+'a'));
            notifyAll();
        }
    }
    public synchronized void printCharA(){
        for(int i=0;i<26;i++){
            while (flag!=3){
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            flag=1;
            System.err.print((char)(count+'A'));
            count++;
            notifyAll();
        }
    }
}

// 3、Condition、CountDownLatch
class Print{
    private int count=0;
    Lock lock = new ReentrantLock();
    Condition c1;
    Condition c2;
    Condition c3;
    CountDownLatch cc1;
    CountDownLatch cc2;
    CountDownLatch cc3;
    public Print1(){
        c1=lock.newCondition();
        c2=lock.newCondition();
        c3=lock.newCondition();
        cc1=new CountDownLatch(1);
        cc2=new CountDownLatch(1);
        cc3=new CountDownLatch(1);
    }
    public void printNum(){
        lock.lock();
        try {
            cc2.countDown();
            for (int i=0;i<26;i++){
                System.err.print(count);
                c2.signal();
                c1.await();
            }
            c2.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }
    public void printChara(){
        try {
            cc2.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        lock.lock();
        try {
            cc3.countDown();
            for (int i=0;i<26;i++){
                System.err.print((char)(count+'a'));
                c3.signal();
                c2.await();
            }
            c3.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
    public void printCharA(){
        try {
            cc3.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        lock.lock();
        try {
            for(int i=0;i<26;i++){
                System.err.print((char)(count+'A'));
                count++;
                c1.signal();
                c3.await();
            }
            c1.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

//4、 信号量
class Print{
    private int count=0;
    Semaphore sN;
    Semaphore sa;
    Semaphore sA;
    public Print3(){
        sN = new Semaphore(1);
        sa = new Semaphore(0);
        sA = new Semaphore(0);
    }
    public void printNum(){
        for(int i=0;i<26;i++){
            try {
                sN.acquire();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.err.print(count);
            sa.release();
        }
    }
    public void printChara(){
        for(int i=0;i<26;i++){
            try {
                sa.acquire();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.err.print((char)(count+'a'));
            sA.release();
        }
    }
    public void printCharA(){
        for(int i=0;i<26;i++){
            try {
                sA.acquire();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.err.print((char)(count+'A'));
            count++;
            sN.release();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值