多线程交替输出问题3种常用解法(多线程经典面试题)

题目:让两个多线程交替输出
举例:“ABCDEF”和“123456”,
输出结果为固定“A1B2C3D4E5F6”

1.利用synchronized+wait/notify

package com.mooc.house.user.controller;

/**
 * @ClassName sync_wait_notify
 * @Description DOTO
 * @Author mischen
 * @Date 2021/9/30 0030 6:11
 * @Version 1.0
 **/
public class sync_wait_notify {
    
    

    public static void main(String[] args) {
    
    
        final Object o = new Object();

        char[] aI = "ABCDEF".toCharArray();
        char[] aC = "123456".toCharArray();


        new Thread(()->{
    
    
            synchronized (o) {
    
    
                for(char c : aI) {
    
    
                    System.out.print(c);
                    try {
    
    
                        o.notify();
                        o.wait(); //让出锁
                    } catch (InterruptedException e) {
    
    
                        e.printStackTrace();
                    }
                }
                o.notify(); //必须,否则无法停止程序
            }

        }, "t1").start();

        new Thread(()->{
    
    
            synchronized (o) {
    
    
                for(char c : aC) {
    
    
                    System.out.print(c);
                    try {
    
    
                        o.notify();
                        o.wait();
                    } catch (InterruptedException e) {
    
    
                        e.printStackTrace();
                    }
                }
                o.notify();
            }
        }, "t2").start();
    }
}

2.利用condition和ReentrantLock联合使用

package com.mooc.house.user.controller;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName T01_00_Question
 * @Description DOTO
 * @Author mischen
 * @Date 2021/9/30 0030 6:17
 * @Version 1.0
 **/
public class T01_00_Question {
    
    

    private char[] a1 = {
    
    '1','2','3','4','5','6'};
    private char[] a2 = {
    
    'A','B','C','D','E','F'};
    private ReentrantLock lock = new ReentrantLock();
    private Condition c1 = lock.newCondition();
    private Condition c2 = lock.newCondition();

    public void m1(){
    
    
        int count = 0;
        lock.lock();
        //这里模拟前面四个数
        while(count <= 5){
    
    
            System.out.print(a2[count++]);
            try {
    
    
                c2.signal();
                c1.await();
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
        }
        c2.signal();
        lock.unlock();
    }

    public void m2(){
    
    
        int count = 0;
        lock.lock();
        while(count <= 5){
    
    
            System.out.print(a1[count++]);
            try {
    
    
                c1.signal();
                c2.await();
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
        }
        c1.signal();
        lock.unlock();
    }
    public static void main(String[] args) {
    
    
        //要求用线程顺序打印A1B2C3....Z26
        T01_00_Question t = new T01_00_Question();
        new Thread(()->{
    
    
            t.m1();
        }).start();
        new Thread(()->{
    
    
            t.m2();
        }).start();
    }
}

3.利用LockSupport

package com.mooc.house.user.controller;

import java.util.concurrent.locks.LockSupport;

/**
 * @ClassName Test1
 * @Description DOTO
 * @Author mischen
 * @Date 2021/9/30 0030 5:42
 * @Version 1.0
 **/
public class Test1 {
    
    

    static Thread t1 = null,t2=null;

    public static void main(String[] args) {
    
    
        char[] a1= "ABCDEF".toCharArray();
        char[] c1= "123456".toCharArray();

        t1 = new Thread(() -> {
    
    
            for(char c : a1) {
    
    
                System.out.print(c);
                LockSupport.unpark(t2); //叫醒T2
                LockSupport.park(); //T1阻塞
            }
        }, "t1");

        t2 = new Thread(() -> {
    
    
            for(char c : c1) {
    
    
                LockSupport.park(); //t2阻塞
                System.out.print(c);
                LockSupport.unpark(t1); //叫醒t1
            }
        }, "t2");

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

Guess you like

Origin blog.csdn.net/miachen520/article/details/120559448