https://blog.csdn.net/qq_42411214/article/details/107767326
一:volatile二:synchronized 临界区方式ReentrantLock/Condition 消息队列方式三:使用JUC工具类 CountDownLatch四:基本LockSupport实现线程间的阻塞和唤醒五:通过Socket网络通信六:信号量机制(Semaphore):包括无名线程信号量和命名线程信号量七:管道通信就是使用java.io.PipedInputStream 和 java.io.PipedOutputStream进行通信
1、waite notify通信
public class Test1 {
private static boolean flag = false;
private static Object lock = new Object();
public static void main(String[] args) {
Wait wait = new Wait();
wait.start();
Notify notify = new Notify();
notify.start();
}
static class Wait extends Thread {
@Override
public void run() {
synchronized (lock) {
while (!flag) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("hello");
}
}
}
static class Notify extends Thread {
@Override
public void run() {
synchronized (lock) {
System.out.println("notify thread start");
lock.notify();
flag = true;
}
}
}
}
。。。