你写一个26字母和0-9数字 不同长度组合(设置一个长度 6 ,比如aabb99,aaaaaaa等是输出内容,顺序不同也算)的输出,输出全部的时间不能超过2个小时。需要用多线程

你写一个26字母和0-9数字 不同长度组合(设置一个长度 6 ,比如aabb99,aaaaaaa等是输出内容,顺序不同也算)的输出,输出全部的时间不能超过2个小时。需要用多线程

 

package dome;

import java.util.concurrent.locks.LockSupport;

/**
 * @author shkstart
 * @create 2022-10-28 17:29
 */
public class Threads {
    private volatile static char[] sum = new char[6];
    private static char[] chars = new char[]{'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','0','1','2','3','4','5','6','7','8','9'};
    private static Thread t1;
    private static Thread t2;
    private static Thread t3;
    private static Thread t4;
    private static Thread t5;
    private static Thread t6;
    //在初始化时创建好6个初始线程
    static {
        t1 = new Thread(
                ()->{
                    for (int i = 0;i<=35;i++){
                        sum[0] = chars[i];
                        //第一次进入是进行睡眠
                        if(i==0){
                            LockSupport.park();
                        }else if (i==35){
                            //最后一次进入时等待下一个线程执行完成然后再创建一个新线程并启动,最后输出结束
                            try {
                                getT2().join();
                                createT2();
                                getT2().start();
                                t2.join();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            System.out.println("结束");
                        }else {
                            //除第一次和最后一次,其他都进入  进入时等待下一个线程执行完成然后再创建一个新线程并启动 当前线程睡眠
                            try {
                                getT2().join();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            createT2();
                            getT2().start();
                            LockSupport.park();
                        }

                    }
                }
        );
        t2 = new Thread(()->{
            for (int i = 0;i<=35;i++){
                sum[1] = chars[i];
                System.out.println(chars[i]+",i="+i);
                if (i==0){
                    createT3();
                    getT3().start();
                    LockSupport.park();
                }else if(i==35){
                    try {
                        getT3().join();
                        createT3();
                        getT3().start();
                        t3.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    LockSupport.unpark(getT1());
                }else {
                    try {
                        getT3().join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    createT3();
                    getT3().start();
                    LockSupport.park();
                }
            }
        });
        t3 = new Thread(
                ()->{
                    for (int i = 0;i<=35;i++){
                        sum[2] = chars[i];
                        if (i==0){
                            createT4();
                            getT4().start();
                            LockSupport.park();
                        }else if(i==35){
                            try {
                                getT4().join();
                                createT4();
                                getT4().start();
                                t4.join();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            LockSupport.unpark(getT2());
                        }else {
                            try {
                                getT4().join();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            createT4();
                            getT4().start();
                            LockSupport.park();
                        }

                    }
                }
        );
        t4 = new Thread(
                ()->{
                    for (int i = 0;i<=35;i++){
                        sum[3] = chars[i];
                        if (i==0){
                            createT5();
                            getT5().start();
                            LockSupport.park();
                        }else if(i==35){
                            try {
                                getT5().join();
                                createT5();
                                getT5().start();
                                t5.join();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            LockSupport.unpark(getT3());
                        }else {
                            try {
                                getT5().join();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            createT5();
                            getT5().start();
                            LockSupport.park();
                        }

                    }
                }
        );
        t5 = new Thread(
                ()->{
                    for (int i = 0;i<=35;i++){
                        sum[4] = chars[i];
                        if (i==0){
                            createT6();
                            getT6().start();
                            LockSupport.park();
                        }else if(i==35){
                            try {
                                getT6().join();
                                createT6();
                                getT6().start();
                                t6.join();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            LockSupport.unpark(getT4());
                        }else {
                            try {
                                getT6().join();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            createT6();
                            getT6().start();
                            LockSupport.park();
                        }

                    }
                }
        );
        t6 = new Thread(
                ()->{
                    for (int i = 0;i<=35;i++){
                        sum[5] = chars[i];
                        String var = "";
                        for (int j = 0; j < sum.length; j++) {
                            var+=sum[j];
                        }
                        System.out.println(var);
                        if (i==35){
                            LockSupport.unpark(getT5());
                        }
                    }
                }
        );
    }
    //在2号线程死亡时再次创建2号线程
    private static Thread createT2(){
        t2 = new Thread(()->{
            for (int i = 0;i<=35;i++){
                sum[1] = chars[i];
                if (i==0){
                    createT3();
                    getT3().start();
                    LockSupport.park();
                }else if(i==35){
                    try {
                        getT3().join();
                        createT3();
                        getT3().start();
                        t3.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    LockSupport.unpark(getT1());
                }else {
                    try {
                        getT3().join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    createT3();
                    getT3().start();
                    LockSupport.park();
                }
            }
        });
        return t2;
    }
    //在3号线程死亡时再次创建3号线程
    private static Thread createT3(){
        t3 = new Thread(
                ()->{
                    for (int i = 0;i<=35;i++){
                        sum[2] = chars[i];
                        if (i==0){
                            createT4();
                            getT4().start();
                            LockSupport.park();
                        }else if(i==35){
                            try {
                                getT4().join();
                                createT4();
                                getT4().start();
                                t4.join();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            LockSupport.unpark(getT2());
                        }else {
                            try {
                                getT4().join();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            createT4();
                            getT4().start();
                            LockSupport.park();
                        }

                    }
                }
        );
        return t3;
    }
    //在4号线程死亡时再次创建4号线程
    private static Thread createT4(){
        t4 = new Thread(
                ()->{
                    for (int i = 0;i<=35;i++){
                        sum[3] = chars[i];
                        if (i==0){
                            createT5();
                            getT5().start();
                            LockSupport.park();
                        }else if(i==35){
                            try {
                                getT5().join();
                                createT5();
                                getT5().start();
                                t5.join();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            LockSupport.unpark(getT3());
                        }else {
                            try {
                                getT5().join();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            createT5();
                            getT5().start();
                            LockSupport.park();
                        }

                    }
                }
        );
        return t4;
    }
    //在5号线程死亡时再次创建5号线程
    private static Thread createT5(){
        t5 = new Thread(
                ()->{
                    for (int i = 0;i<=35;i++){
                        sum[4] = chars[i];
                        if (i==0){
                            createT6();
                            getT6().start();
                            LockSupport.park();
                        }else if(i==35){
                            try {
                                getT6().join();
                                createT6();
                                getT6().start();
                                t6.join();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            LockSupport.unpark(getT4());
                        }else {
                            try {
                                getT6().join();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            createT6();
                            getT6().start();
                            LockSupport.park();
                        }

                    }
                }
        );
        return t5;
    }
    //在6号线程死亡时再次创建6号线程
    private static Thread createT6(){
        t6 = new Thread(
                ()->{
                    for (int i = 0;i<=35;i++){
                        sum[5] = chars[i];
                        String var = "";
                        for (int j = 0; j < sum.length; j++) {
                            var+=sum[j];
                        }
                        System.out.println(var);
                        if (i==35){
                            LockSupport.unpark(getT5());
                        }
                    }
                }
        );
        return t6;
    }
    //对外提供线程
    public static Thread getT1() {
        return t1;
    }

    public static Thread getT2() {
        return t2;
    }

    public static Thread getT3() {
        return t3;
    }

    public static Thread getT4() {
        return t4;
    }

    public static Thread getT5() {
        return t5;
    }

    public static Thread getT6() {
        return t6;
    }
}

 使用

Threads t = new Threads();
t.getT1().start();
t.getT2().start();
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值