兩個線程交替打印
public class Test2 { private static int count = 0; private final static Object lock = new Object(); static class TurningRunner implements Runnable { @Override public void run() { while (count <= 10) { // 獲取鎖 synchronized (lock) { // 拿到鎖就打印 System.out.println(Thread.currentThread().getName() + ":" + count++); // 喚醒其他線程 lock.notifyAll(); try { // 如果任務還沒有結束,則讓出當前的鎖並休眠 if (count <= 10) { lock.wait(); } } catch (InterruptedException e) { e.printStackTrace(); } } } } } public static void main(String[] args) throws InterruptedException { Thread a = new Thread(new TurningRunner(), "偶數"); Thread b = new Thread(new TurningRunner(), "奇數"); a.start(); b.start(); } }
三個線程交替打印
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class PrintNumber extends Thread {
/**
* 多個線程共享這一個sequence數據
*/
private static int sequence = 1;
private static final int SEQUENCE_END =75;
private int id;
private static ReentrantLock lock = new ReentrantLock();
private static Condition[] conditions = {lock.newCondition(),lock.newCondition(),lock.newCondition()};
private PrintNumber(int id) {
this.id = id;
this.setName("線程:" + (id + 1));
}
@Override
public void run() {
while (sequence < SEQUENCE_END) {
lock.lock();
try {
//對序號取模,如果不等於當前線程的id,則先喚醒其他線程,然后當前線程進入等待狀態
while ((sequence/5) % conditions.length != id) {
conditions[(id + 1) % conditions.length].signal();
conditions[id].await();
}
for(int i = 0; i < 5; i++)
{
System.out.println(Thread.currentThread().getName() + " " + sequence++);
}
//喚醒當前線程的下一個線程
conditions[(id + 1) % conditions.length].signal();
//當前線程進入等待狀態
conditions[id].await();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
//將釋放鎖的操作放到finally代碼塊中,保證鎖一定會釋放
lock.unlock();
}
}
//數字打印完畢,線程結束前喚醒其余的線程,讓其他線程也可以結束
end();
}
private void end() {
lock.lock();
conditions[(id + 1) % conditions.length].signal();
conditions[(id + 2) % conditions.length].signal();
lock.unlock();
}
public static void main(String[] args) {
int threadCount = 3;
// ReentrantLock lock = new ReentrantLock();
// Condition[] conditions = new Condition[threadCount];
// for (int i = 0; i < threadCount; i++) {
// conditions[i] = lock.newCondition();
// }
PrintNumber[] printNumbers = new PrintNumber[threadCount];
for (int i = 0; i < threadCount; i++) {
printNumbers[i] = new PrintNumber(i);
}
for (PrintNumber printNumber : printNumbers) {
printNumber.start();
}
}
}