三个线程实现顺序打印0aA1bB
public class Main {
static Thread threadA,threada,threadN;
public static void main(String[] args) {
Print2 print = new Print2();
threadA = new Thread(()->{
for(int i=0;i<26;i++){
LockSupport.park();
print.printCharA();
LockSupport.unpark(threadN);
}
});
threadN = new Thread(()->{
for(int i=0;i<26;i++){
print.printNum();
LockSupport.unpark(threada);
LockSupport.park();
}
});
threada = new Thread(()->{
for(int i=0;i<26;i++){
LockSupport.park();
print.printChara();
LockSupport.unpark(threadA);
}
});
threadA.start();
threadN.start();
threada.start();
}
}
public class Main {
public static void main(String[] args) {
Print print = new Print();
new Thread(print::printChara).start();
new Thread(print::printNum).start();
new Thread(print::printCharA).start();
System.err.print("\n");
}
}
class Print{
private int flag=1;
private int count=0;
public synchronized void printNum(){
for(int i=0;i<26;i++){
while (flag!=1){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
flag=2;
System.err.print(count);
notifyAll();
}
}
public synchronized void printChara(){
for(int i=0;i<26;i++){
while (flag!=2){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
flag=3;
System.err.print((char)(count+'a'));
notifyAll();
}
}
public synchronized void printCharA(){
for(int i=0;i<26;i++){
while (flag!=3){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
flag=1;
System.err.print((char)(count+'A'));
count++;
notifyAll();
}
}
}
class Print{
private int count=0;
Lock lock = new ReentrantLock();
Condition c1;
Condition c2;
Condition c3;
CountDownLatch cc1;
CountDownLatch cc2;
CountDownLatch cc3;
public Print1(){
c1=lock.newCondition();
c2=lock.newCondition();
c3=lock.newCondition();
cc1=new CountDownLatch(1);
cc2=new CountDownLatch(1);
cc3=new CountDownLatch(1);
}
public void printNum(){
lock.lock();
try {
cc2.countDown();
for (int i=0;i<26;i++){
System.err.print(count);
c2.signal();
c1.await();
}
c2.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void printChara(){
try {
cc2.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
lock.lock();
try {
cc3.countDown();
for (int i=0;i<26;i++){
System.err.print((char)(count+'a'));
c3.signal();
c2.await();
}
c3.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void printCharA(){
try {
cc3.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
lock.lock();
try {
for(int i=0;i<26;i++){
System.err.print((char)(count+'A'));
count++;
c1.signal();
c3.await();
}
c1.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
class Print{
private int count=0;
Semaphore sN;
Semaphore sa;
Semaphore sA;
public Print3(){
sN = new Semaphore(1);
sa = new Semaphore(0);
sA = new Semaphore(0);
}
public void printNum(){
for(int i=0;i<26;i++){
try {
sN.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.print(count);
sa.release();
}
}
public void printChara(){
for(int i=0;i<26;i++){
try {
sa.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.print((char)(count+'a'));
sA.release();
}
}
public void printCharA(){
for(int i=0;i<26;i++){
try {
sA.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.print((char)(count+'A'));
count++;
sN.release();
}
}
}