如何实现一个线程输出数字一个线程输出字母交替执行

要求:实现一个线程输出数字一个线程输出字母交替执行

方式一、通过LockSupport方式

    static Thread t_char=null;
    static Thread t_num=null;

    public static void main(String[] args) {
        char []num="123456789".toCharArray();
        char []chars="abcdefghi".toCharArray();
        way1(num,chars);
    }

/**
     * 方案一,通过LockSupport
     * @param num
     * @param chars
     */
    public static void way1(char[]num,char []chars){
        t_num=new Thread(()->{
            for(char c:num){
                System.out.print(c);
                LockSupport.unpark(t_char);
                LockSupport.park();
            }

        });

        t_char=new Thread(()->{
            for(char c:chars){
                LockSupport.park();
                System.out.print(c);
                LockSupport.unpark(t_num);
            }
        });

        t_char.start();
        t_num.start();
    }

方式二、通过Synchronized关键字、Object的notify()和wait()方法

static Thread t_char=null;
    static Thread t_num=null;

    public static void main(String[] args) {
        char []num="123456789".toCharArray();
        char []chars="abcdefghi".toCharArray();
        way2(num,chars);
    }
    /**
     * 方案二,通过Object的wait()和notify()方法
     * @param num
     * @param chars
     */
    public static void way2(char[]num,char []chars){
         Object lock= new Object();
        t_num=new Thread(()->{
            synchronized (lock){
                for(char c:num){
                    System.out.print(c);
                    try {
                        lock.notify();
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }


        });

        t_char=new Thread(()->{
            synchronized (lock){
                for(char c:chars){
                    try {
                        lock.wait();//让出锁
                        lock.notify();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.print(c);
                }
            }
        });

        t_char.start();
        t_num.start();
    }

方式三、通过ReentrantLock和Condition

static Thread t_char = null;
    static Thread t_num = null;

    public static void main(String[] args) {
        char[] num = "123456789".toCharArray();
        char[] chars = "abcdefghi".toCharArray();
        way3(num, chars);
    }

    /**
     * 方案三,通过ReentrantLock和Condition
     * @param num
     * @param chars
     */
    public static void way3(char[] num, char[] chars) {
        ReentrantLock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        t_num = new Thread(() -> {
            for (char c : num) {
                System.out.print(c);
                try {
                    lock.lock();
                    condition.signal();
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }
            }
        });

        t_char = new Thread(() -> {
            for (char c : chars) {
                try {
                    lock.lock();
                    condition.await();
                    condition.signal();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }
                System.out.print(c);
            }
        });

        t_char.start();
        t_num.start();
    }

方案四、通过LinkedTransferQueue

static Thread t_char=null;
    static Thread t_num=null;

    public static void main(String[] args) {
        char []num="123456789".toCharArray();
        char []chars="abcdefghi".toCharArray();
        way4(num,chars);
    }

    /**
     * 方案四,通过LinkedTransferQueue
     * @param num
     * @param chars
     */
    public static void way4(char[]num,char []chars){
        LinkedTransferQueue queue=new LinkedTransferQueue<Character>();

        t_num=new Thread(()->{
            for(char c:num){
                try {
                    //往queue中放
                    queue.transfer(c);
                    //等待别的线程拿,如果不拿走,就等待
                    System.out.print(queue.take());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        t_char=new Thread(()->{
            for(char c:chars){
                try {
                    //从queue中取,如果没有则等待
                    System.out.print(queue.take());
                    queue.transfer(c);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        t_char.start();
        t_num.start();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值