使用ReentrantLock+condition 实现线程交替打印
package customer; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; /** * @Author lizhilong * @create 2020/7/6 22:39 * @desc */ public class ExchangePrint2 { public static void main(String[] args) throws InterruptedException { ReentrantLock rLock = new ReentrantLock(); Condition condition = rLock.newCondition(); Condition condition1 = rLock.newCondition(); new Thread(()->{ char a = 'A'; try { for(int i=0;i<26;i++){ rLock.lock(); System.out.println(a); Thread.sleep(1000); a++; condition1.signal(); condition.await(); } }catch (Exception e){ e.printStackTrace(); }finally { rLock.tryLock(); } }).start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(()->{ try { for(int i=1;i<27;i++){ rLock.lock(); System.out.println(i); Thread.sleep(1000); condition.signal(); condition1.await(); } }catch (Exception e){ e.printStackTrace(); }finally { rLock.tryLock(); } }).start(); } }