package com.java265.other; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; public class Test6_1 { static Thread t2 = null; static Thread t1 = null; public static void main(String[] args) throws Exception { ReentrantLock rt = new ReentrantLock(); Condition l1 = rt.newCondition(); Condition l2 = rt.newCondition(); t1 = new Thread(() -> { rt.lock(); for (int i = 0; i < 26; i++) { char c = (char) (i + (int) 'a'); try { l2.signal(); l1.await(); System.out.print(c); } catch (InterruptedException e) { e.printStackTrace(); } } l2.signal(); rt.unlock(); }); t2 = new Thread(() -> { rt.lock(); for (int i = 1; i <= 26; i++) { try { l1.signal(); l2.await(); System.out.print(i + " "); } catch (InterruptedException e) { e.printStackTrace(); } } l1.signal(); rt.unlock(); }); t1.start(); t2.start(); } } -----运行以上代码,将输出以下信息---- a1 b2 c3 d4 e5 f6 g7 h8 i9 j10 k11 l12 m13 n14 o15 p16 q17 r18 s19 t20 u21 v22 w23 x24 y25 z26
扫VX 领Java资料,前端,测试,python等等资料都有