public class PrintABC_Condition {
private Lock lock = new ReentrantLock();
private Condition conditionA = lock.newCondition();
private Condition conditionB = lock.newCondition();
private Condition conditionC = lock.newCondition();
private char currThreadName = 'A';
public void printABC(){
System.out.println();
Thread threadA = new Thread(new ThreadA());
Thread threadB = new Thread(new ThreadB());
Thread threadC = new Thread(new ThreadC());
threadA.start();
threadB.start();
threadC.start();
}
private class ThreadA implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; ++i) {
lock.lock();
try {
while (currThreadName != 'A') {
try {
conditionA.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print("A");
currThreadName = 'B';
conditionB.signal();
}finally {
lock.unlock();
}
}
}
}
private class ThreadB implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; ++i) {
lock.lock();
try {
while (currThreadName != 'B') {
try {
conditionB.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print("B");
currThreadName = 'C';
conditionC.signal();
}finally {
lock.unlock();
}
}
}
}
private class ThreadC implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; ++i) {
lock.lock();
try {
while (currThreadName != 'C') {
try {
conditionC.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print("C");
currThreadName = 'A';
conditionA.signal();
}finally {
lock.unlock();
}
}
}
}
}
public class PrintABC_Syn {
private char currThreadName = 'A';
public PrintABC_Syn() {
}
public void printABC() {
Thread threadA = new Thread(new PrintATask());
Thread threadB = new Thread(new PrintBTask());
Thread threadC = new Thread(new PrintCTask());
threadA.start();
threadB.start();
threadC.start();
}
private synchronized void printA() {
try {
while (currThreadName != 'A') {
wait();
}
System.out.print("A");
currThreadName = 'B';
notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
notifyAll();
}
}
private synchronized void printB() {
try {
while (currThreadName != 'B') {
wait();
}
System.out.print("B");
currThreadName = 'C';
notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
notifyAll();
}
}
private synchronized void printC() {
try {
while (currThreadName != 'C') {
wait();
}
System.out.print("C");
currThreadName = 'A';
notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
notifyAll();
}
}
private class PrintATask implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; ++i){
printA();
}
}
}
private class PrintBTask implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; ++i){
printB();
}
}
}
private class PrintCTask implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; ++i){
printC();
}
}
}
}
public class PrintABC_Lock {
private static final int MAXN = 30;
private AtomicInteger sycValue = new AtomicInteger(0);
public void printABC(){
Thread threadA = new Thread(new ABCRunnable());
Thread threadB = new Thread(new ABCRunnable());
Thread threadC = new Thread(new ABCRunnable());
threadA.start();
threadB.start();
threadC.start();
}
private class ABCRunnable implements Runnable{
@Override
public void run() {
while (sycValue.get() < MAXN){
if (sycValue.get() % 3 == 0){
System.out.print("A");
sycValue.getAndIncrement();
}else if (sycValue.get() % 3 == 1){
System.out.print("B");
sycValue.getAndIncrement();
}else if (sycValue.get() % 3 == 2) {
System.out.print("C");
sycValue.getAndIncrement();
}
}
}
}
}
1. 请使用Java提供的synchronized块、wait()、notify()和notifyAll()方法,实现三个并发执行的线程1、2和3协同输出ABCABC…ABC这样的字符串序列(序列中ABC重复出现10次),其中线程1负责打印输出字母A,线程2负责打印输出字母B,线程3负责打印输出字母C。