实现打印26个数字和26个字母交替打印

0、面试题

实现:第一个线程从1到26,第二个线程从A到Z,然后要让这两个线程做到同时运行,交替输出,顺序打印。

输出结果:1A2B3C4D5E6F7G8H9I10J11K12L13M14N15O16P17Q18R19S20T21U22V23W24X25Y26Z

你有哪些玩法?

1、LockSupport

public class LockSupportDemo {
    
    static Thread t1 = null, t2 = null;

    public static void main(String[] args) {
        String[] nums = new String[]{
                "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26"
        };
        String[] letters = new String[]{
                "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"
        };

        t1 = new Thread(() -> {
            for (String num : nums) {
                System.out.print(num);
                LockSupport.unpark(t2);//唤醒 t2
                LockSupport.park();// t1 阻塞
            }
        }, "T1");

        t2 = new Thread(() -> {
            for (String letter : letters) {
                LockSupport.park();// t2 阻塞
                System.out.print(letter);
                LockSupport.unpark(t1);//唤醒 t1
            }
        }, "T2");

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

2、synchronized+wait+notify

public class WaitNotifyDemo {

    static Thread t1 = null, t2 = null;


    public static void main(String[] args) {

        final Object o = new Object();

        String[] nums = new String[]{
                "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26"
        };
        String[] letters = new String[]{
                "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"
        };

        t1 = new Thread(() -> {
            synchronized (o) {
                try {
                    for (String num : nums) {
                        System.out.print(num);
                        o.notify();
                        o.wait(); //阻塞 t1
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                o.notify();//必须,否则无法停止程序
            }
        }, "T1");

        t2 = new Thread(() -> {
            synchronized (o) {
                try {
                    for (String letter : letters) {
                        System.out.print(letter);
                        o.notify();
                        o.wait(); //阻塞 t2
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                o.notify(); //必须,否则无法停止程序
            }
        }, "T2");

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

3、ReentrantLock + 1个Condition

public class LockConditionDemo {

    static Thread t1 = null, t2 = null;

    public static void main(String[] args) {

        String[] nums = new String[]{
                "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26"
        };
        String[] letters = new String[]{
                "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"
        };

        ReentrantLock lock = new ReentrantLock();

        Condition condition = lock.newCondition();

        t1 = new Thread(() -> {
            lock.lock();
            try {
                for (String num : nums) {
                    System.out.print(num);
                    condition.signal();
                    condition.await();
                }
                condition.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }

        }, "T1");

        t2 = new Thread(() -> {

            lock.lock();
            try {
                for (String letter : letters) {
                    System.out.print(letter);
                    condition.signal();
                    condition.await();
                }
                condition.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }

        }, "T2");

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

4、ReentrantLock + 2个Condition

public class LockConditionDemo2 {

    static Thread t1 = null, t2 = null;

    public static void main(String[] args) {

        String[] nums = new String[]{
                "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26"
        };
        String[] letters = new String[]{
                "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"
        };

        ReentrantLock lock = new ReentrantLock();

        Condition conditionT1 = lock.newCondition();
        Condition conditionT2 = lock.newCondition();

        t1 = new Thread(() -> {
            lock.lock();
            try {
                for (String num : nums) {
                    System.out.print(num);
                    conditionT2.signal();
                    conditionT1.await();
                }
                conditionT2.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }

        }, "T1");

        t2 = new Thread(() -> {

            lock.lock();
            try {
                for (String letter : letters) {
                    System.out.print(letter);
                    conditionT1.signal();
                    conditionT2.await();
                }
                conditionT1.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }

        }, "T2");

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

5、自定义CAS实现

public class CASDemo {

    enum ReadyToRun {T1, T2}

    static volatile ReadyToRun r = ReadyToRun.T1;

    static Thread t1 = null, t2 = null;

    public static void main(String[] args) {
        String[] nums = new String[]{
                "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26"
        };
        String[] letters = new String[]{
                "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"
        };

        t1 = new Thread(() -> {
            for (String num : nums) {
                while (r != ReadyToRun.T1) {
                }
                System.out.print(num);
                r = ReadyToRun.T2;
            }
        }, "T1");

        t2 = new Thread(() -> {
            for (String letter : letters) {
                while (r != ReadyToRun.T2) {
                }
                System.out.print(letter);
                r = ReadyToRun.T1;
            }
        }, "T2");

        t1.start();
        t2.start();

    }

}

6、Semaphore

public class SemaphoreDemo {

    static Thread t1 = null, t2 = null;

    private static Semaphore semaphore1 = new Semaphore(1);
    private static Semaphore semaphore2 = new Semaphore(0);

    public static void main(String[] args) {
        String[] nums = new String[]{
                "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26"
        };
        String[] letters = new String[]{
                "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"
        };

        t1 = new Thread(() -> {
            try {
                for (String num : nums) {

                    semaphore1.acquire();
                    System.out.print(num);
                    semaphore2.release();

                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "T1");

        t2 = new Thread(() -> {
            try {
                for (String letter : letters) {
                    semaphore2.acquire();
                    System.out.print(letter);
                    semaphore1.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "T2");

        t1.start();
        t2.start();

    }

}

7、BlockingQueue

public class BlockingQueueDemo {

    static BlockingQueue<String> queue1 = new ArrayBlockingQueue<>(1);
    static BlockingQueue<String> queue2 = new ArrayBlockingQueue<>(1);

    static Thread t1 = null, t2 = null;

    public static void main(String[] args) {

        String[] nums = new String[]{
                "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26"
        };
        String[] letters = new String[]{
                "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"
        };

        t1 = new Thread(() -> {
            try {
                for (String num : nums) {
                    System.out.print(num);
                    queue1.put("ok");
                    queue2.take();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "T1");

        t2 = new Thread(() -> {
            try {
                for (String letter : letters) {
                    queue1.take();
                    System.out.print(letter);
                    queue2.put("ok");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "T2");

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

}

8、TransferQueue

public class TransferQueueDemo {

    static Thread t1 = null, t2 = null;

    public static void main(String[] args) {
        String[] nums = new String[]{
                "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26"
        };
        String[] letters = new String[]{
                "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"
        };

        TransferQueue<String> queue = new LinkedTransferQueue<>();

        t1 = new Thread(() -> {
            try {
                for (String num : nums) {
                    queue.transfer(num);
                    System.out.print(queue.take());
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "T1");

        t2 = new Thread(() -> {
            try {
                for (String letter : letters) {
                    System.out.print(queue.take());
                    queue.transfer(letter);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "T2");

        t1.start();
        t2.start();
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 夜莺2517阅读 127,829评论 1 9
  • 版本:ios 1.2.1 亮点: 1.app角标可以实时更新天气温度或选择空气质量,建议处女座就不要选了,不然老想...
    我就是沉沉阅读 11,866评论 1 6
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 12,755评论 28 53
  • 兔子虽然是枚小硕 但学校的硕士四人寝不够 就被分到了博士楼里 两人一间 在学校的最西边 靠山 兔子的室友身体不好 ...
    待业的兔子阅读 7,535评论 2 9

友情链接更多精彩内容