线程间通信的模型有两种:共享内存和消息传递,以下方式都是基本这两种模型来实现的。
1. 案例:加一减一
两个线程,一个线程对当前数值加 1,另一个线程对当前数值减 1,要求用线程间通信。
1.1 synchronized 方案
/**
* synchronized 关键字实现线程交替加减
*/
public class TestJUC {
public static void main(String[] args) {
DemoClass demoClass = new DemoClass();
new Thread(() -> {
for (int i = 0; i < 5; i++) {
demoClass.increment();
}
}, "线程 A").start();
new Thread(() -> {
for (int i = 0; i < 5; i++) {
demoClass.decrement();
}
}, "线程 B").start();
}
}
class DemoClass {
//加减对象
private int number = 0;
/**
* 加 1
*/
public synchronized void increment() {
try {
while (number != 0) {
this.wait();
}
number++;
System.out.println("--------" + Thread.currentThread().getName() + "加一成功----------,值为:" + number);
notifyAll();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 减 1
*/
public synchronized void decrement() {
try {
while (number == 0) {
this.wait();
}
number--;
System.out.println("--------" + Thread.currentThread().getName() + "减一成功----------,值为:" + number);
notifyAll();
} catch (Exception e) {
e.printStackTrace();
}
}
}
1.2 Lock方案
class DemoClass {
//加减对象
private int number = 0;
//声明锁
private Lock lock = new ReentrantLock();
//声明钥匙
private Condition condition = lock.newCondition();
/**
* 加 1
*/
public void increment() {
try {
lock.lock();
while (number != 0) {
condition.await();
}
number++;
System.out.println("--------" + Thread.currentThread().getName() + "加一成功----------,值为:" + number);
condition.signalAll();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
/**
* 减 1
*/
public void decrement() {
try {
lock.lock();
while (number == 0) {
condition.await();
}
number--;
System.out.println("--------" + Thread.currentThread().getName() + "减一成功----------,值为:" + number);
condition.signalAll();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
2. 案例:多个线程轮询输出
A 线程打印 5 次 A,B 线程打印 10 次 B,C 线程打印 15 次 C,按照此顺序循环 10 轮。
public class TestJUC {
public static void main(String[] args) {
DemoClass demoClass = new DemoClass();
new Thread(() -> {
for (int i = 1; i <= 10; i++) {
demoClass.printA(i);
}
}, "A 线程").start();
new Thread(() -> {
for (int i = 1; i <= 10; i++) {
demoClass.printB(i);
}
}, "B 线程").start();
new Thread(() -> {
for (int i = 1; i <= 10; i++) {
demoClass.printC(i);
}
}, "C 线程").start();
}
}
class DemoClass {
//通信对象:0--打印 A 1---打印 B 2----打印 C
private int number = 0;
//声明锁
private Lock lock = new ReentrantLock();
//声明钥匙 A
private Condition conditionA = lock.newCondition();
//声明钥匙 B
private Condition conditionB = lock.newCondition();
//声明钥匙 C
private Condition conditionC = lock.newCondition();
/**
* A 打印 5 次
*/
public void printA(int j) {
try {
lock.lock();
while (number != 0) {
conditionA.await();
}
System.out.println(Thread.currentThread().getName() + "输出 A,第" + j + "轮开始");
//输出 5 次 A
for (int i = 0; i < 5; i++) {
System.out.println("A");
}
//开始打印 B
number = 1;
//唤醒 B
conditionB.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
/**
* B 打印 10 次
*/
public void printB(int j) {
try {
lock.lock();
while (number != 1) {
conditionB.await();
}
System.out.println(Thread.currentThread().getName() + "输出 B,第" + j + "轮开始");
//输出 10 次 B
for (int i = 0; i < 10; i++) {
System.out.println("B");
}
//开始打印 C
number = 2;
//唤醒 C
conditionC.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
/**
* C 打印 15 次
*/
public void printC(int j) {
try {
lock.lock();
while (number != 2) {
conditionC.await();
}
System.out.println(Thread.currentThread().getName() + "输出 C,第" + j + "轮开始");
//输出 15 次 C
for (int i = 0; i < 15; i++) {
System.out.println("C");
}
System.out.println("-----------------------------------------");
//开始打印 A
number = 0;
//唤醒 A
conditionA.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
3. synchronized实现同步的基础
Java中每个对象都可以作为锁,具体表现为一下3中形式:
- 对于普通同步方法,锁是当前实例对象。
- 对于静态同步方法,锁是当前类的Class对象。
- 对于同步方法块,锁是Synchronized括号里配置的对象。