import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.*;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
@Slf4j
public class ThreadDemo {
public static void main(String[] args) {
lockFunc();
}
@SneakyThrows
public static void lockFunc(){
ExecutorService executorService0 = Executors.newCachedThreadPool();
WaitNotifyExample waitNotifyExample = new WaitNotifyExample();
executorService0.execute(() -> waitNotifyExample.after());
executorService0.execute(() -> waitNotifyExample.before());
LockExample s1 = new LockExample();
ExecutorService executorService = Executors.newCachedThreadPool();
log.info("小明和小芳10米接力跑开始!");
executorService.execute(() -> s1.func1("小明"));
executorService.execute(() -> s1.func1("小芳"));
executorService.shutdown();
Thread.sleep(1000);
log.info("然后小明和小芳开始10米随机交叉跑!");
LockExample s2 = new LockExample();
LockExample s3 = new LockExample();
ExecutorService executorService2 = Executors.newCachedThreadPool();
executorService2.execute(() -> s2.func2("小明"));
executorService2.execute(() -> s3.func2("小芳"));
executorService2.shutdown();
Thread.sleep(1000);
log.info("后来教练说必须接力跑!");
LockExample s4 = new LockExample();
LockExample s5 = new LockExample();
ExecutorService executorService3 = Executors.newCachedThreadPool();
executorService3.execute(() -> s4.func3("小明"));
executorService3.execute(() -> s5.func3("小芳"));
executorService3.shutdown();
Thread.sleep(1000);
log.info("这项规定变成固定规则,不管是谁都要接力跑!");
LockExample s6 = new LockExample();
LockExample s7 = new LockExample();
ExecutorService executorService4 = Executors.newCachedThreadPool();
executorService4.execute(() -> s6.func4("小蓝"));
executorService4.execute(() -> s7.func4("小白"));
executorService4.shutdown();
Thread.sleep(1000);
log.info("下面开始拿着接力棒训练");
LockExample s8 = new LockExample();
ExecutorService executorService5 = Executors.newCachedThreadPool();
executorService5.execute(() -> s8.func5("小蓝",10));
executorService5.execute(() -> s8.func5("小白",10));
executorService5.shutdown();
Thread.sleep(1000);
ExecutorService executorService6 = Executors.newCachedThreadPool();
AwaitSignalExample awaitSignalExample = new AwaitSignalExample();
executorService6.execute(() -> awaitSignalExample.after());
executorService6.execute(() -> awaitSignalExample.before());
Thread.sleep(1000);
JoinExample example = new JoinExample();
example.test();
}
static class LockExample{
public void func1(String name){
synchronized (this){
for(int i=1;i<=10;i++){
log.info(name+"跑了"+i+"米!");
}
}
}
public synchronized void func2(String name){
synchronized (this){
for(int i=1;i<=10;i++){
log.info(name+"跑了"+i+"米!");
}
}
}
public void func3(String name){
synchronized (LockExample.class){
for(int i=1;i<=10;i++){
log.info(name+"跑了"+i+"米!");
}
}
}
public synchronized static void func4(String name){
for(int i=1;i<=10;i++){
log.info(name+"跑了"+i+"米!");
}
}
private Lock lock = new ReentrantLock();
public void func5(String name,Integer n){
lock.lock();
try{
for (int i=0;i<n;i++){
log.info(name+"正在进行"+n+"米跑!");
}
} finally {
lock.unlock(); // 确保释放锁,从而避免发生死锁。
}
}
}
public static class JoinExample {
private class A extends Thread {
@SneakyThrows
@Override
public void run() {
System.out.println("小兰说等我穿上鞋!");
Thread.sleep(1000);
System.out.println("小兰终于穿好了鞋!");
}
}
private class B extends Thread {
private A a;
B(A a) {
this.a = a;
}
@Override
public void run() {
System.out.println("柯南说我们出发吧!");
try {
a.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("柯南和小兰一起回家了!");
}
}
public void test() {
A a = new A();
B b = new B(a);
b.start();
a.start();
}
}
public static class WaitNotifyExample {
public synchronized void before() {
System.out.println("教练到了之后说今天的训练开始!");
notifyAll();
}
public synchronized void after() {
try {
System.out.println("同学们都已经到了操场等教练");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("然后开始了接力跑训练!");
}
}
public static class AwaitSignalExample {
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public void before() {
lock.lock();
try {
System.out.println("教练说解散!");
condition.signalAll();
} finally {
lock.unlock();
}
}
public void after() {
lock.lock();
try {
System.out.println("天很晚了,同学们等教练说解散!");
condition.await();
System.out.println("同学们高兴的回家了!");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
}