多线程_交替打印

面试题:

两个线程交替打印26个英文字母和数字
1A2B3C…26Z

方法一 park unpark
public class Thread_two_thread_06_26 {

    static Thread t1;
    static Thread t2;

    public static void main(String[] args) {


        List<Integer> numList = new ArrayList<>();


        for (int i = 1; i < 27; i++) {
            numList.add(i);
        }

        List<String> yingwenNum = new ArrayList<>();

        char a[] = new char[26];
        for (int i = 0; i < 26; i++) {
            char c = (char) ('A' + i);
            yingwenNum.add(String.valueOf(c));
        }



        t1 = new Thread(()->{
            numList.forEach((o)->{

                System.out.println(o);
                LockSupport.unpark(t2);
                LockSupport.park();

            });
        });




        t2 = new Thread(()->{
            yingwenNum.forEach((o)->{

                LockSupport.park();
                System.out.println(o);
                LockSupport.unpark(t1);

            });
        });

        t1.start();
        sleep(1);
        t2.start();
    }


    private static void sleep(Integer sleepTime){
        try{
            Thread.sleep(sleepTime);
        }catch (Exception e){

        }
    }

}
方法二:synchronized wait notify
public class Thread_two_thread_07_26 {

    static Thread t1;
    static Thread t2;

    public static void main(String[] args) {

        Object obj = new Object();

        List<Integer> numList = new ArrayList<>();
        List<String> yingwenNum = new ArrayList<>();
        get26English(numList, yingwenNum);


        new Thread(()->{
            yingwenNum.forEach((o)->{
                try {
                    synchronized(obj){
                        obj.wait();
                        System.out.println(o);
                        obj.notify();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });

        }).start();

        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(()->{
            numList.forEach((o)->{
                try {
                    synchronized(obj){
                        System.out.println(o);
                        obj.notify();
                        obj.wait();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });

        }).start();


    }

    public static void get26English(List<Integer> numList, List<String> yingwenNum) {

        for (int i = 1; i < 27; i++) {
            numList.add(i);
        }
        for (int i = 0; i < 26; i++) {
            char c = (char) ('A' + i);
            yingwenNum.add(String.valueOf(c));
        }
    }
}
方法三:exchanger (不能完全实现交替打印)
public class Thread_two_thread_08_26 {

    public static void main(String[] args) {

        Exchanger<Object> exchanger = new Exchanger<>();

        Thread_two_thread_07_26 th = new Thread_two_thread_07_26();

        List<Integer> numList = new ArrayList<>();
        List<String> yingwenNum = new ArrayList<>();
        th.get26English(numList, yingwenNum);

        new Thread(()->{
            numList.forEach((o)->{
                try{
                    Object exchange = exchanger.exchange(o);
                    System.out.println(exchange);
                }catch (Exception e){

                }

            });
        }).start();


        new Thread(()->{
            yingwenNum.forEach((o)->{

                try{
                    Object exchange = exchanger.exchange(o);
                    System.out.println(exchange);
                }catch (Exception e){

                }
            });
        }).start();

    }
}

在这里插入图片描述

此处感谢评论区大佬指出错误@感性企鹅

优化版(我就要实现)
public class Thread_two_thread_08_26 {

    public static void main(String[] args) {

        Exchanger<Object> exchanger = new Exchanger<>();

        Thread_two_thread_07_26 th = new Thread_two_thread_07_26();

        List<Integer> numList = new ArrayList<>();
        List<String> yingwenNum = new ArrayList<>();
        th.get26English(numList, yingwenNum);

        new Thread(()->{
            numList.forEach((o)->{
                try{
                    Object exchange = exchanger.exchange(o);
                    Thread.sleep(1);  //需要睡一下,来保证交替打印
                    System.out.println(exchange);
                }catch (Exception e){

                }

            });
        }).start();


        new Thread(()->{
            yingwenNum.forEach((o)->{

                try{
                    Object exchange = exchanger.exchange(o);
                    System.out.println(exchange);
                }catch (Exception e){

                }
            });
        }).start();

    }
}

1
A
2
B
3
C
4
D …

在两个线程输出时,一个线程睡一下就可以了
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值