交替输出两个数组的元素
使用Synchronized wait notify
import java.util.concurrent.CountDownLatch;
public class PrintInTurnTest02 {
public static void main(String[] args) {
final Object lock = new Object();
String a1 = "1234567";
String a2 = "ABCDEFG";
char[] digit = a1.toCharArray();
char[] chars = a2.toCharArray();
CountDownLatch latch = new CountDownLatch(1);
new Thread(() -> {
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock) {
for (Character c : digit) {
System.out.println(c);
try {
lock.notify();
lock.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
lock.notify();
}
}).start();
new Thread(() -> {
synchronized (lock) {
for (Character c : chars) {
System.out.println(c);
latch.countDown();
try {
lock.notify();
lock.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
lock.notify();
}
}).start();
}
}
使用Reentrantlock await signal
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class PrintInTurnTest03 {
public static void main(String[] args) {
String a1 = "1234567";
String a2 = "ABCDEFG";
char[] digit = a1.toCharArray();
char[] chars = a2.toCharArray();
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
CountDownLatch latch = new CountDownLatch(1);
new Thread(() -> {
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
lock.lock();
try {
for (char c : digit) {
System.out.println(c);
condition.signal();
condition.await();
}
condition.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}).start();
new Thread(() -> {
lock.lock();
try {
for (char c : chars) {
System.out.println(c);
latch.countDown();
condition.signal();
condition.await();
}
condition.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}).start();
}
}
交替打印0-100
使用Reentrantlock await signal AtomicInteger
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class PrintInTurnTest04 {
public static void main(String[] args) {
AtomicInteger num = new AtomicInteger(0);
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
new Thread(() -> {
lock.lock();
try {
while (num.get() < 100) {
num.getAndIncrement();
System.out.println(Thread.currentThread().getName() + ": " + num);
condition.signal();
condition.await();
}
condition.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}).start();
new Thread(() -> {
lock.lock();
try {
while (num.get() < 100) {
num.getAndIncrement();
System.out.println(Thread.currentThread().getName() + ": " + num);
condition.signal();
condition.await();
}
condition.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}).start();
}
}
三个线程交替输出ABC
使用ReentrantLock condition
package com.example.interview.multi_thread.print_in_turn;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class PrintInTurnTest05 {
public static void main(String[] args) {
Lock lock = new ReentrantLock();
Condition printA = lock.newCondition();
Condition printB = lock.newCondition();
Condition printC = lock.newCondition();
new Thread(() -> {
lock.lock();
try {
while (true) {
System.out.println(Thread.currentThread().getId() + "A");
printB.signal();
printA.await();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}).start();
new Thread(() -> {
lock.lock();
try {
while (true) {
System.out.println(Thread.currentThread().getId() + "B");
printC.signal();
printB.await();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}).start();
new Thread(() -> {
lock.lock();
try {
while (true) {
System.out.println(Thread.currentThread().getId() + "C");
printA.signal();
printC.await();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}).start();
}
}
轮流打印奇偶数
synchronized
package com.example.interview.multi_thread.print_in_turn;
import java.util.concurrent.atomic.AtomicInteger;
public class PrintInTurnTest06 {
static class PrintOdd implements Runnable {
AtomicInteger count;
Object lock;
public PrintOdd(AtomicInteger count, Object lock) {
this.count = count;
this.lock = lock;
}
@Override
public void run() {
while (count.get() <= 100) {
synchronized (lock) {
if (count.get() % 2 == 1) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
System.out.println(Thread.currentThread().getName() + " " + count);
count.getAndIncrement();
lock.notify();
}
}
}
}
}
static class PrintEven implements Runnable {
AtomicInteger count;
Object lock;
public PrintEven(AtomicInteger count, Object lock) {
this.count = count;
this.lock = lock;
}
@Override
public void run() {
while (count.get() <= 100) {
synchronized (lock) {
if (count.get() % 2 == 0) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
System.out.println(Thread.currentThread().getName() + " " + count);
count.getAndIncrement();
lock.notify();
}
}
}
}
}
public static void main(String[] args) {
Object lock = new Object();
AtomicInteger count = new AtomicInteger(1);
new Thread(new PrintEven(count, lock)).start();
new Thread(new PrintOdd(count, lock)).start();
}
}