概述
1. 交叉打印AB
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class ABDemo {
}
class ABPrint {
private boolean flag = false;
public synchronized void printA() {
while (flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("A");
flag = true;
this.notifyAll();
}
public synchronized void printB() {
while (!flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("B");
flag = false;
this.notifyAll();
}
public static void main(String[] args) {
ABPrint demo = new ABPrint();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
demo.printA();
}
}, "A").start();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
demo.printB();
}
}, "B").start();
}
}
class BAPrint {
private volatile boolean flag = false;
private Object o = new Object();
public void printA() {
synchronized (o) {
while (flag) {
try {
o.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
flag = true;
System.out.println("A");
o.notify();
}
}
public void printB() {
synchronized (o) {
while (!flag) {
try {
o.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("B");
flag = false;
o.notify();
}
}
public static void main(String[] args) {
BAPrint demo = new BAPrint();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
demo.printA();
}
}, "A").start();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
demo.printB();
}
}, "B").start();
}
}
class LockDemo{
private ReentrantLock mylock = new ReentrantLock();
private Condition condition = mylock.newCondition();
private volatile boolean flag = false;
public void printA() {
try {
mylock.lock();
while (flag) {
condition.await();
}
System.out.println("A");
flag = true;
condition.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
mylock.unlock();
}
}
public void printB() {
try {
mylock.lock();
while (!flag) {
condition.await();
}
System.out.println("B");
flag = false;
condition.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
mylock.unlock();
}
}
public static void main(String[] args) {
LockDemo demo = new LockDemo();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
demo.printA();
}
}, "A").start();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
demo.printB();
}
}, "B").start();
}
}
2. 交叉打印ABC
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ABC_LockCondition {
private static Lock lock = new ReentrantLock();
private static Condition A = lock.newCondition();
private static Condition B = lock.newCondition();
private static Condition C = lock.newCondition();
private static int count = 0;
static class ThreadA extends Thread {
@Override
public void run() {
try {
lock.lock();
for (int i = 0; i < 10; i++) {
while (count % 3 != 0)
A.await();
// 释放lock锁,ThreadC 会唤醒该线程继续执行
System.out.println("A");
count++;
B.signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
static class ThreadB extends Thread {
@Override
public void run() {
try {
lock.lock();
for (int i = 0; i < 10; i++) {
while (count % 3 != 1)
B.await();
System.out.println("B");
count++;
C.signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
static class ThreadC extends Thread {
@Override
public void run() {
try {
lock.lock();
for (int i = 0; i < 10; i++) {
while (count % 3 != 2) {
C.await();
}
System.out.println("C");
count++;
A.signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
public static void main(String[] args) {
new ThreadA().start();
new ThreadB().start();
new ThreadC().start();
}
}
最后
以上就是文艺鼠标为你收集整理的《2021/03/22》多线程编码的全部内容,希望文章能够帮你解决《2021/03/22》多线程编码所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复