zoukankan      html  css  js  c++  java
  • 实现两个线程顺序依次打印元素

    public class L_ConditionTest {
        public static void main(String[] args) {
            L_ConditionTest t = new L_ConditionTest();
            // 这两个方法都有问题,假设两个打印的数组长度不对称,那会造成一个线程始终卡死
            t.test();
            t.test2();
            // 那么假设两个数组长度不一致,那该如何处理呢??
        }
        /**
         * 使用 SynchronousQueue 进行线程阻塞
         */
        private void test2() {
            char[] number = "12345678".toCharArray();
            char[] word = "ABCDEfgh".toCharArray();
            SynchronousQueue<String> queue = new SynchronousQueue<>();
            new Thread(() -> {
    
                for (char c : number) {
                    System.out.print(c);
                    try {
                        queue.put("X");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    // 先释放对方 在阻塞自己
                }
    
            }, "t1").start();
    
            new Thread(() -> {
    
                for (char c : word) {
                    try {
                        queue.take();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.print(c);
                }
    
            }, "t2").start();
        }
    
        /**
         * 使用ReentrantLock 的 Condition 
         */
        private void test() {
            char[] number = "123456789".toCharArray();
            char[] word = "ABCDEfgh".toCharArray();
            Lock lock = new ReentrantLock();
            Condition numberCondition = lock.newCondition();
            Condition wordCondition = lock.newCondition();
    
            new Thread(() -> {
    
                lock.lock();
                try {
                    for (char c : number) {
                        System.out.print(c);
                        // 先释放对方 在阻塞自己
                        wordCondition.signalAll();
                        numberCondition.await();
                    }
                    wordCondition.signalAll();
    
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }, "t1").start();
    
            new Thread(() -> {
                lock.lock();
                try {
                    for (char c : word) {
                        System.out.print(c);
                        numberCondition.signalAll();
                        wordCondition.await();
                    }
                    numberCondition.signalAll();
    
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
    
            }, "t2").start();
    
        }
    }
    
  • 相关阅读:
    表的转置 行转列: DECODE(Oracle) 和 CASE WHEN 的异同点
    Sql中EXISTS与IN的使用及效率
    Oracle学习之start with...connect by子句的用法
    Java复制、移动和删除文件
    简单的实现微信获取openid
    SQL语句中LEFT JOIN、JOIN、INNER JOIN、RIGHT JOIN的区别?
    java 基础最全网站
    SpringBoot(十一)过滤器和拦截器
    做项目遇到的问题集锦
    使用Java实现二叉树的添加,删除,获取以及遍历
  • 原文地址:https://www.cnblogs.com/huan30/p/13233146.html
Copyright © 2011-2022 走看看