public class Test {
public static void main(String[] args) throws Exception {
AtomicBoolean isA = new AtomicBoolean(true);
new Thread(() -> {
for (int i = 0; i < 10; ) {
while (isA.get()) {
System.out.print("A");
i++;
isA.set(false);
}
Thread.yield();
}
}).start();
new Thread(() -> {
for (int i = 0; i < 10; ) {
while (!isA.get()) {
System.out.print("B");
i++;
isA.set(true);
}
Thread.yield();
}
}).start();
}
}
2.synchronized+标志位
public class Test {
public static void main(String[] args) throws Exception {
Object lock = new Object();
AtomicBoolean isA = new AtomicBoolean(true);
new Thread(() -> {
for (int i = 0; i < 10;) {
synchronized (lock){
while (!isA.get()) {
try {
lock.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.print("A");
i++;
isA.set(false);
lock.notifyAll();
}
}
}).start();
new Thread(() -> {
for (int i = 0; i < 10;) {
synchronized (lock) {
while (isA.get()) {
try {
lock.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.print("B");
i++;
isA.set(true);
lock.notifyAll();
}
}
}).start();
}
}
3.lock公平锁+条件队列+标志位
public class Test {
public static void main(String[] args) throws Exception {
ReentrantLock lock = new ReentrantLock(true);
Condition condition = lock.newCondition();
AtomicBoolean isA = new AtomicBoolean(true);
new Thread(() -> {
for (int i = 0; i < 10; i++) {
lock.lock();
try {
while (!isA.get()) {
//不是自己打印的标志就释放锁
try {
condition.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.print("A");
isA.set(false);
condition.signalAll();
} finally {
lock.unlock();
}
}
}).start();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
lock.lock();
try {
while (isA.get()) {
//不是自己打印的标志就释放锁
try {
condition.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.print("B");
isA.set(true);
condition.signalAll();
} finally {
lock.unlock();
}
}
}).start();
}
}
4.阻塞队列
public class Test {
public static void main(String[] args) {
BlockingQueue<Integer> queueA = new LinkedBlockingQueue<>(1);
BlockingQueue<Integer> queueB = new LinkedBlockingQueue<>(1);
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
queueA.put(i);
System.out.print("A");
queueB.put(i);
}catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}).start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
queueB.take();
System.out.print("B");
queueA.take();
}catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}).start();
}
}
5.信号量
public class Test {
public static void main(String[] args) {
Semaphore semaphoreA = new Semaphore(1);
Semaphore semaphoreB = new Semaphore(0);
new Thread(() -> {
for (int i = 0; i < 10; i++) {
try {
semaphoreA.acquire();
System.out.print("A");
semaphoreB.release();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}).start();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
try {
semaphoreB.acquire();
System.out.print("B");
semaphoreA.release();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}).start();
}
}
6.CyclicBarrier等待
public class Test {
public static void main(String[] args) {
CyclicBarrier barrier = new CyclicBarrier(2);//循环的countdownLatch
AtomicBoolean isA = new AtomicBoolean(true);
new Thread(() -> {
for (int i = 0; i < 10; i++) {
while (!isA.get()) {
}
System.out.print("A");
isA.set(false);
try {
barrier.await();//等待所有线程执行完
} catch (InterruptedException | BrokenBarrierException e) {
throw new RuntimeException(e);
}
}
}).start();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
while (isA.get()) {
}
System.out.print("B");
isA.set(true);
try {
barrier.await();//等待所有线程执行完
} catch (InterruptedException | BrokenBarrierException e) {
throw new RuntimeException(e);
}
}
}).start();
}
}