Java多线程04——线程通信
1 线程通信机制
线程通信指的是不同线程之间可以交换一些实时的数据信息。
线程是操作系统中的独立个体,但这些个体如果不经过特殊处理就不能成为一个整体,线程间的通信就成为整体的必用方式之一。例如之前处理的线程同步,就是一种线程间通信的方式。
当线程存在通信指挥,系统间的交互性会更强大,在提高CPU利用率的同时,还会使开发人员对线程任务在处理过程中进行有效的把控与监督。
实现线程间的通信方法:
wait
/notify
这两个方法都是Object类的方法,换句话说,Java为所有的对象都提供了这两个方法。
Lock
和Condition
从JAVA5开始,提供了 Lock
机制,同时还有用于处理 Lock
机制通信控制的 Condition
接口。
2 线程通信的 wait
和 notify
机制
- 等待/通知机制
是指线程A调用了对象的 wait()
方法进入到等待状态,而线程B调用了对象的 notify()
或 notifyAll()
方法,线程A收到通知后退出等待队列,进入到可运行状态,进而执行后续操作。
- 从功能上来说,
wait
就是线程在获取对象锁后,主动释放对象锁,同时本线程休眠。直到有其它线程调用对象的notify()
唤醒该线程,才能继续获取对象锁,并继续执行。 wait()
和notify()
方法是Object的方法,即所有对象都拥有该方法。public class PrintChar {
private boolean flag = false;
public void printA(){
synchronized (this) {
while (true) {
if(flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " 输出A");
flag = true;
this.notify();
}
}
}
public void printB(){
synchronized (this) {
while (true) {
if(!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " 输出B");
flag = false;
this.notify();
}
}
}
}
class Thread1 extends Thread {
PrintChar pc;
public Thread1(PrintChar pc){
this.pc = pc;
}
@Override
public void run() {
pc.printA();
}
}
class Thread2 extends Thread {
PrintChar pc;
public Thread2(PrintChar pc){
this.pc = pc;
}
@Override
public void run() {
pc.printB();
}
}
复制代码
测试类
public class TestWait {
public static void main(String[] args) {
PrintChar pc = new PrintChar();
new Thread1(pc).start();
new Thread2(pc).start();
}
}
复制代码
输出效果如下:
Thread-0 输出A
Thread-1 输出B
Thread-0 输出A
Thread-1 输出B
Thread-0 输出A
Thread-1 输出B
Thread-0 输出A
Thread-1 输出B
Thread-0 输出A
Thread-1 输出B
过程如下图所示:
3 线程通信的 Lock
和 Condition
机制
Lock 用于控制多线程对需要竞争的共享资源的顺序访问,保证该状态的连续性。
Condition 是 Java 提供的来实现等待/通知的类,Condition 对象是由 Lock 对象所创建。
Condition 中的 await()
方法相当于 Object 的 wait()
方法,Condition 中的 signal()
方法相当于 Object 的 notify()
方法。
使用 Lock
和 Condition
机制改写上边的类
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class PrintChar2 {
private boolean flag = false;
//创建锁
Lock lock = new ReentrantLock();
//创建监视器
Condition condition = lock.newCondition();
public void printA(){
while (true) {
lock.lock();
if(flag){
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " 输出A");
flag = true;
condition.signal();
lock.unlock();
}
}
public void printB(){
while (true) {
lock.lock();
if(!flag){
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " 输出B");
flag = false;
condition.signal();
lock.unlock();
}
}
}
class Thread11 extends Thread {
PrintChar2 pc;
public Thread11(PrintChar2 pc){
this.pc = pc;
}
@Override
public void run() {
pc.printA();
}
}
class Thread22 extends Thread {
PrintChar2 pc;
public Thread22(PrintChar2 pc){
this.pc = pc;
}
@Override
public void run() {
pc.printB();
}
}
复制代码
测试类
public class TestWait {
public static void main(String[] args) {
PrintChar2 pc = new PrintChar2();
new Thread11(pc).start();
new Thread22(pc).start();
}
}
复制代码
输出效果如下:
Thread-0 输出A
Thread-1 输出B
Thread-0 输出A
Thread-1 输出B
Thread-0 输出A
Thread-1 输出B
Thread-0 输出A
Thread-1 输出B
Thread-0 输出A
Thread-1 输出B
需要注意的是: 如果使用 Lock 实现锁机制,需要替换原来的 synchronized 代码块,同时加锁和解锁都需要手动操作。二者略有不同。
还没有评论,来说两句吧...