多线程按序交替打印
Posted ch-forever
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程按序交替打印相关的知识,希望对你有一定的参考价值。
一、线程按序交替打印
效果展示:
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();
}
}
以上是关于多线程按序交替打印的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口