开启两个线程,一个线程打印A~Z,一个线程打印1~52的数据

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; public class D20190803 { private char a='A';
private Lock lock = new ReentrantLock();
private Condition c_one=lock.newCondition();
private Condition c_two=lock.newCondition();
private static boolean flag=true; Thread letter=new Thread(new Runnable() { public void run() { for (int i=0;i<26;i++) { lock.lock(); try { if(flag) { c_two.await(); } System.out.print("【"+a+"】"); a++; Thread.sleep(100);
flag=true;
c_one.signal(); } catch (InterruptedException e) { e.printStackTrace();
}finally { lock.unlock();
} } }
}); Thread number=new Thread(new Runnable() { public void run() { for (int j=1;j<53;j++) { lock.lock(); System.out.print(j+","); if(j%2==0 ) { try { Thread.sleep(100);
c_two.signal();
flag=false; if(!flag) { c_one.await();
} } catch (InterruptedException e) { e.printStackTrace();
}finally { lock.unlock();
} } } } }); public static void main(String[] args) throws InterruptedException { D20190803 d=new D20190803();
d.letter.start();
Thread.sleep(1000);
d.number.start(); } }

开启两个线程,一个线程打印A~Z,一个线程打印1~52的数据-LMLPHP

05-11 13:58