package com.example.demo.test;
import org.junit.Test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;
public class TestABC {
@Test
public void t1() {
CountDownLatch count = new CountDownLatch(2);
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("线程01开始");
count.countDown();
System.out.println("线程01-1");
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("线程02开始");
count.countDown();
System.out.println("线程02-1");
}
}).start();
try {
System.out.println("主线程等待");
count.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (count.getCount() == 0) {
System.out.println("主线程工作");
}
}
/**
* 多线程循环打印AB
* sync + wait + notify
*/
final static Object lock = new Object();
@Test
public void t2() {
CountDownLatch countDownLatch = new CountDownLatch(10);
Thread t1, t2 = null;
t1 = new Thread(new Runnable() {
@Override
public void run() {
while (countDownLatch.getCount() != 0) {
synchronized (lock) {
countDownLatch.countDown();
System.out.println("Thread-1 - A");
try {
lock.notify();
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
t2 = new Thread(new Runnable() {
@Override
public void run() {
while (countDownLatch.getCount() != 0) {
synchronized (lock) {
countDownLatch.countDown();
System.out.println("Thread-2 - B");
try {
lock.notify();
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
t1.start();
t2.start();
try {
countDownLatch.await();
System.out.println(countDownLatch.getCount());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
static Thread t1, t2, t3;
static Lock lock1 = new ReentrantLock();
static Condition conditionA = lock1.newCondition();
static Condition conditionB = lock1.newCondition();
static Condition conditionC = lock1.newCondition();
@Test
public void t4() {
t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
lock1.lock();
System.out.print("A ");
conditionB.signalAll();
try {
conditionA.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
lock1.unlock();
}
}
});
t2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
lock1.lock();
try {
conditionB.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print("B ");
conditionC.signalAll();
lock1.unlock();
}
}
});
t3 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
lock1.lock();
try {
conditionC.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("C");
conditionA.signalAll();
lock1.unlock();
}
}
});
t3.start();
t2.start();
t1.start();
}
@Test
public void t5() {
t1 = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
System.out.print("A");
LockSupport.unpark(t2);
LockSupport.park();
}
}
});
t2 = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
LockSupport.park();
System.out.println("B");
LockSupport.unpark(t1);
}
}
});
t1.start();
t2.start();
}
public static void main(String[] args) {
t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.print("A ");
LockSupport.unpark(t2);
LockSupport.park(t1);
}
}
});
t2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
LockSupport.park();
System.out.print("B ");
LockSupport.unpark(t3);
}
}
});
t3 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
LockSupport.park();
System.out.println("C");
LockSupport.unpark(t1);
}
}
});
t3.start();
t2.start();
t1.start();
}
}
多线程打印ABABC