多线程实现互斥
package com.startx.http.wordfilter.thread;
import sun.awt.windows.ThemeReader;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
//三种方式实现 刷碗和洗碗操作
public class ThreadDemo2 {
public static void main1(String[] args) {
Object obj = new Object();
Thread t2 = new Thread(() -> {
synchronized (obj) {
for (int i = 6; i >= 1; i--) {
System.err.println(i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.println(i);
if (i == 1) {
obj.notifyAll();
}
}
}
});
Thread t1 = new Thread(() -> {
synchronized (obj) {
t2.start();
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.println("t1 is finished!!");
}
});
t1.start();
}
//ReentrantLock Condition 同步 代替synchronized
public static void main(String[] args) {
ReentrantLock lock = new ReentrantLock();
Condition condition = lock.newCondition();
Message message = new Message();
new Thread(() -> {
lock.lock();
for (int i = 0; i < 10; i++) {
System.err.println(Thread.currentThread() + " " + i);
if (message.getId() == 0) {
message.setId(1);
System.err.println(Thread.currentThread().getName() + " " + message.getId());
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
condition.signal();
}
}
lock.unlock();
}).start();
new Thread(() -> {
lock.lock();
for (int i = 0; i < 10; i++) {
System.err.println(Thread.currentThread() + " " + i);
if (message.getId() != 0) {
message.setId(0);
System.err.println(Thread.currentThread().getName() + " " + message.getId());
condition.signal();
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
lock.unlock();
}).start();
}
//CountDownLatch
public static void mainByCountDownLatch(String[] args) {
CountDownLatch countDownLatch = new CountDownLatch(1);
Message message = new Message();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
System.err.println(Thread.currentThread() + " " + i);
if (message.getId() == 0) {
message.setId(1);
System.err.println(Thread.currentThread().getName() + " " + message.getId());
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
countDownLatch.countDown();
}
}
}).start();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
System.err.println(Thread.currentThread() + " " + i);
if (message.getId() != 0) {
message.setId(0);
System.err.println(Thread.currentThread().getName() + " " + message.getId());
countDownLatch.countDown();
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
//同步
public static void mainByWaitAndNotify(String[] args) {
Message message = new Message();
new Thread(() -> {
synchronized (message) {
for (int i = 0; i < 10; i++) {
System.err.println(Thread.currentThread() + " " + i);
if (message.getId() == 0) {
message.setId(1);
System.err.println(Thread.currentThread().getName() + " " + message.getId());
try {
message.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
//如果不加 notify 那么当前线程会等待另一个调用notify的线程执行完毕后才会再执行此线程
message.notify();
}
}
}
}).start();
new Thread(() -> {
synchronized (message) {
for (int i = 0; i < 10; i++) {
System.err.println(Thread.currentThread() + " " + i);
if (message.getId() != 0) {
message.setId(0);
System.err.println(Thread.currentThread().getName() + " " + message.getId());
message.notifyAll();
try {
message.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}).start();
}
}