一、线程按序交替打印
效果展示:
A 1
A 2
A 3
A 4
A 5
B 1
B 2
B 3
B 4
B 5
C 1
C 2
C 3
C 4
C 5
代码实现:
package juc;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class AlternativePrint {
private ReentrantLock lock = new ReentrantLock();
private Condition conditionA = lock.newCondition();
private Condition conditionB = lock.newCondition();
private Condition conditionC = lock.newCondition();
private int number = 1;
public void loopA(){
lock.lock();
try {
if (number != 1){
conditionA.await();
}
for (int i = 1; i <= 5; i++){
System.out.println(Thread.currentThread().getName() + " " + i);
}
number = 2;
conditionB.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void loopB(){
lock.lock();
try {
if (number != 2){
conditionB.await();
}
for (int i = 1; i <= 5; i++){
System.out.println(Thread.currentThread().getName() + " " + i);
}
number = 3;
conditionC.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void loopC(){
lock.lock();
try {
if (number != 3){
conditionC.await();
}
for (int i = 1; i <= 5; i++){
System.out.println(Thread.currentThread().getName() + " " + i);
}
number = 1;
conditionA.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("--------------------------------------------------");
lock.unlock();
}
}
public static void main(String[] args) {
final AlternativePrint alternativePrint = new AlternativePrint();
new Thread("A"){
@Override
public void run() {
for (int i= 0; i < 20; i++)
alternativePrint.loopA();
}
}.start();
new Thread("B"){
@Override
public void run() {
for (int i= 0; i < 20; i++)
alternativePrint.loopB();
}
}.start();
new Thread("C"){
@Override
public void run() {
for (int i= 0; i < 20; i++)
alternativePrint.loopC();
}
}.start();
}
}
二、多线程交替打印10次abc实例
效果展示:
A 1
B 1
C 1
-----------------------------------------------------------------
A 1
B 1
C 1
-----------------------------------------------------------------
代码实现:
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class AlternativePrint {
private ReentrantLock lock = new ReentrantLock();
private Condition conditionA = lock.newCondition();
private Condition conditionB = lock.newCondition();
private Condition conditionC = lock.newCondition();
private int number = 1;
public void loopA(){
lock.lock();
try {
if (number != 1){
conditionA.await();
}
for (int i = 1; i <= 1; i++){
System.out.println(Thread.currentThread().getName() + " " + i);
}
number = 2;
conditionB.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void loopB(){
lock.lock();
try {
if (number != 2){
conditionB.await();
}
for (int i = 1; i <= 1; i++){
System.out.println(Thread.currentThread().getName() + " " + i);
}
number = 3;
conditionC.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void loopC(){
lock.lock();
try {
if (number != 3){
conditionC.await();
}
for (int i = 1; i <= 1; i++){
System.out.println(Thread.currentThread().getName() + " " + i);
}
number = 1;
conditionA.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("-----------------------------------------------");
lock.unlock();
}
}
public static void main(String[] args) {
final AlternativePrint alternativePrint = new AlternativePrint();
new Thread("A"){
@Override
public void run() {
for (int i= 0; i < 20; i++)
alternativePrint.loopA();
}
}.start();
new Thread("B"){
@Override
public void run() {
for (int i= 0; i < 20; i++)
alternativePrint.loopB();
}
}.start();
new Thread("C"){
@Override
public void run() {
for (int i= 0; i < 20; i++)
alternativePrint.loopC();
}
}.start();
}
}
原文地址:https://www.cnblogs.com/ch-forever/p/10293741.html
时间: 2024-11-12 21:06:51