给定两个列表,使用线程同步之打印1A2B3C4D5E6F7G8H9I10J...

    科技2023-10-08  73

    线程同步

    1.题目解析2.线程同步demo2.1LockSupport Demo2.2自旋 Demo2.3阻塞队列 Demo2.4 synchronized Demo2.5阻塞队列 Demo结果

    1.题目解析

    循环打印出,肯定需要两个进程,如何保障两个进程有序打印呢?这就涉及到的线程同步问题。

    使用同步锁的帮助类:LockSupport 两个重要方法 //唤醒指定线程 public static void unpark(Thread thread)//阻塞当前进程 public static void park()

    使用自旋 占用cpu时间;

    使用阻塞队列: BlockingDeque

    // 将指定的元素插入此deque表示的队列中 put(E e); // 检索并删除此deque表示的队列头 take(); 使用synchronized,notify,wait使用lock,condition; //使用condition必须先lock(); Lock lock = new ReentrantLock(); Condition condition = lock.newCondition(); // 唤醒一个等待线程。该线程从等待方法返回前必须获得与Condition相关的锁。 condition.signal(); // 造成当前线程在接到信号或被中断之前一直处于等待状态 condition.wait();

    2.线程同步demo

    2.1LockSupport Demo

    public clsaa Demo { static Thread t1= null; static Thread t2 = null; public static void main(String[] args) throws InterruptedException { List<Integer> numberList = Lists.newArrayList(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); List<String> stringList = Lists.newArrayList("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"); List<String> resultList = Lists.newArrayListWithExpectedSize(numberList.size()+stringList.size()); t1 = new Thread(() -> { for (Integer integer : numberList) { resultList.add(String.valueOf(integer)); //唤醒指定阻塞线程 LockSupport.unpark(t2); //阻塞当前线程 LockSupport.park(); } }); t2 = new Thread(() -> { for (String str : stringList) { //阻塞当前线程 LockSupport.park(); resultList.add(str); //唤醒指定阻塞线程 LockSupport.unpark(t1); } }); t1.start(); t2.start(); //阻塞主线程一会 Thread.sleep((long) (10 * 10)); System.out.println(resultList); System.out.println(resultList.stream().collect(Collectors.joining())); } }

    2.2自旋 Demo

    public clsaa Demo { static Thread t1= null; static Thread t2 = null; enum ThreadToRun { T1, T2 } static volatile ThreadToRun threadToRun = ThreadToRun.T1; public static void main(String[] args) throws InterruptedException { List<Integer> numberList = Lists.newArrayList(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); List<String> stringList = Lists.newArrayList("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"); List<String> resultList = Lists.newArrayListWithExpectedSize(numberList.size() + stringList.size()); t1 = new Thread(() -> { for (Integer integer : numberList) { //如果不是T1线程,就一直循环 while (threadToRun != ThreadToRun.T1) {} resultList.add(String.valueOf(integer)); threadToRun = ThreadToRun.T2; } }); t2 = new Thread(() -> { for (String str : stringList) { while (threadToRun != ThreadToRun.T2) {} resultList.add(String.valueOf(str)); threadToRun = ThreadToRun.T1; } }); t1.start(); t2.start(); //阻塞主线程一会 Thread.sleep((long) (10 * 100)); System.out.println(resultList); System.out.println(String.join("", resultList)); }

    2.3阻塞队列 Demo

    2.4 synchronized Demo

    public static void main(String[] args) throws InterruptedException { List<Integer> numberList = Lists.newArrayList(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); List<String> stringList = Lists.newArrayList("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"); List<String> resultList = Lists.newArrayListWithExpectedSize(numberList.size() + stringList.size()); Object o = new Object(); t1 = new Thread(() -> { synchronized (o) { for (Integer integer : numberList) { resultList.add(String.valueOf(integer)); try { //唤醒队列中的一个线程 o.notify(); //进入等待队列,当前线程让出锁 o.wait(); } catch (Exception e) { } } o.notify(); } }); t2 = new Thread(() -> { synchronized (o) { for (String str : stringList) { resultList.add(str); try { //唤醒队列中的一个线程 o.notify(); //进入等待队列,当前线程让出锁 o.wait(); } catch (Exception e) { } } //必须,否则程序无法停止 o.notify(); } }); t1.start(); t2.start(); //阻塞主线程一会 Thread.sleep((long) (10 * 100)); System.out.println(resultList); System.out.println(String.join("", resultList)); }

    2.5阻塞队列 Demo

    结果

    Processed: 0.018, SQL: 8