【面试题】两个线程分别打印26个英文字母的元音(a,e,i,o,u)和辅音(其他),按字母序输出

两个线程分别打印26个英文字母的元音(a,e,i,o,u)和辅音(其他),按字母序输出

package com.xiaochengxinyizhan.data_structures.codingCode;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 小诚信驿站
 * @create 2019-06-24 9:51
 **/
public class Words26Test {

    //实现公平锁,依次交替打印26字母
    public static ReentrantLock lock = new ReentrantLock(true);

    //共计26字母
    public static String[] words = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};

    public static volatile int wordIndex = 0;

    public static void main(String args[]) {
        //声明两个线程,如果是元音字母则A线程输出,如果是辅音字母则B线程输出
        TA a = new TA();
        Thread tA = new Thread(a);
        TB b = new TB();
        Thread tB = new Thread(b);
        //启动的时候抢夺CPU时间分片资源不一定谁先抢到,所以设定初始字母判断
        if (words.length <= 0) {
            System.out.println("请完善数组,数组不能问为空!");
            return;
        }
        tA.start();
        tB.start();

    }

    private static boolean isYuanWord(String word) {
        if ("a".equals(word)) {
            return true;
        } else if ("e".equals(word)) {
            return true;
        } else if ("i".equals(word)) {
            return true;
        } else if ("o".equals(word)) {
            return true;
        } else if ("u".equals(word)) {
            return true;
        } else {
            return false;
        }
    }

    private static class TA implements Runnable {

        @Override
        public void run() {
            wordIndex = 0;
            while (wordIndex < words.length) {
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {

                }
                //先加锁

                lock.lock();
                System.out.println("我是线程TA:抢夺上锁");
                //执行线程TA只输出元音字母,其他字母则跳出循环
                //循环数组
                try {
                    for (; wordIndex < words.length; wordIndex++) {
                        if (isYuanWord(words[wordIndex])) {
                            System.out.println("我是线程TA:输出的元音字母为:" + words[wordIndex]);
                        } else {
                            break;
                        }
                    }
                } catch (Exception e) {

                } finally {
                    lock.unlock();
                    System.out.println("我是线程TA:释放锁");
                }
            }
        }
    }


private static class TB implements Runnable {

    @Override
    public void run() {

        wordIndex = 0;
        while (wordIndex < words.length) {
            try {
                Thread.sleep(1000);
            } catch (Exception e) {

            }
            //先加锁
            lock.lock();
            System.out.println("我是线程TB:抢夺上锁");
            //执行线程TA只输出元音字母,其他字母则跳出循环
            //循环数组
            try {
                for (; wordIndex < words.length; wordIndex++) {
                    if (!isYuanWord(words[wordIndex])) {
                        System.out.println("我是线程TB:输出的辅音字母为:" + words[wordIndex]);
                    } else {
                        break;
                    }
                }
            } catch (Exception e) {

            } finally {
                lock.unlock();
                System.out.println("我是线程TB:释放锁");
            }
        }
    }
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小诚信驿站

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值