再以CountDownLatch以例,任务分为N个子线程去执行,state也初始化为N(注意N要与线程个数一致)。这N个子线程是并行执行的,每个子线程执行完后countDown()一次,state会CAS减1。等到所有子线程都执行完后(即state=0),会unpark()调用线程,然后主调用线程就会从await()函数返回,继续后余动作。
package com.bfxy.thread.core.juc;
import java.util.concurrent.CountDownLatch;
public class UseCountDownLatch {
public static void main(String[] args) {
CountDownLatch countDownLatch = new CountDownLatch(2);
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
System.err.println("进入t1线程..");
try {
Thread.sleep(3000); //做了一些初始化的准备..
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.println("t1线程初始化完毕, 通知t3 线程继续操作");
countDownLatch.countDown();
}
}, "t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
System.err.println("进入t2线程..");
try {
Thread.sleep(4000); //做了一些初始化的准备..
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.println("t2线程初始化完毕, 通知t3 线程继续操作");
countDownLatch.countDown();
}
}, "t2");
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
System.err.println("进入t3线程.., 并且进入等待...");
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.println("t3线程 进行后续的操作!");
}
}, "t3");
t1.start();
t2.start();
t3.start();
}
}
AQS-Condition:
还记得我们在使用synchronized的时候,如果需要多线程间进行协作工作则需要Object的
wait()和notify()、notifyAll()方法进行配合工作。
那么同样,我们在使用Lock的时候,可以使用一个新的等待/通知的类,它就是Condition。
这个Condition一定是针对具体某一把锁的。也就是在只有锁的基础之上才会产生Condition。
我们可以通过一个Lock对象产生多个Condition进行多线程间的交互,非常的灵活。可以使得
部分需要唤醒的线程唤醒,其他线程则继续等待通知。
一个condition:
condition.signal();//发信号让另外一个等待线程继续执行
package com.bfxy.thread.core.aqs;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class UseCondition {
//现在有一把锁
private Lock lock = new ReentrantLock();
//synchronized wait ---- notify
//基于这把锁产生了一个 condition: 作用是对于这把锁的 唤醒 和 等待操作
private Condition condition = lock.newCondition();
public void method1(){
lock.lock();
try {
System.out.println("当前线程:" + Thread.currentThread().getName() + "进入等待状态..");
Thread.sleep(3000);
System.out.println("当前线程:" + Thread.currentThread().getName() + "释放锁..");
condition.await();
System.out.println("当前线程:" + Thread.currentThread().getName() +"继续执行...");
} catch (Exception e) {
e.printStackTrace();
} finally {
System.err.println(Thread.currentThread().getName() + " unlock");
lock.unlock();
}
}
public void method2(){
lock.lock();
try {
System.out.println("当前线程:" + Thread.currentThread().getName() + "进入..");
Thread.sleep(3000);
System.out.println("当前线程:" + Thread.currentThread().getName() + "发出唤醒..");
condition.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public static void main(String[] args) throws Exception {
final UseCondition uc = new UseCondition();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
uc.method1();
}
}, "t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
uc.method2();
}
}, "t2");
t1.start();
Thread.sleep(1);
t2.start();
}
}
多个condition:
c1.signalAll();//唤醒所有与c1有关的线程继续执行
package com.bfxy.thread.core.aqs;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class UseManyCondition {
private Lock lock = new ReentrantLock();
private Condition c1 = lock.newCondition();
private Condition c2 = lock.newCondition();
public void m1(){
try {
lock.lock();
System.out.println("当前线程:" +Thread.currentThread().getName() + "进入方法m1等待..");
c1.await();
System.out.println("当前线程:" +Thread.currentThread().getName() + "方法m1继续..");
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void m2(){
try {
lock.lock();
System.out.println("当前线程:" +Thread.currentThread().getName() + "进入方法m2等待..");
c1.await();
System.out.println("当前线程:" +Thread.currentThread().getName() + "方法m2继续..");
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void m3(){
try {
lock.lock();
System.out.println("当前线程:" +Thread.currentThread().getName() + "进入方法m3等待..");
c2.await();
System.out.println("当前线程:" +Thread.currentThread().getName() + "方法m3继续..");
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void m4(){
try {
lock.lock();
System.out.println("当前线程:" +Thread.currentThread().getName() + "唤醒..");
c1.signalAll();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void m5(){
try {
lock.lock();
System.out.println("当前线程:" +Thread.currentThread().getName() + "唤醒..");
c2.signalAll();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public static void main(String[] args) {
final UseManyCondition umc = new UseManyCondition();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
umc.m1();
}
},"t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
umc.m2();
}
},"t2");
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
umc.m3();
}
},"t3");
Thread t4 = new Thread(new Runnable() {
@Override
public void run() {
umc.m4();
}
},"t4");
Thread t5 = new Thread(new Runnable() {
@Override
public void run() {
umc.m5();
}
},"t5");
t1.start();
t2.start();
t3.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t4.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t5.start();
}
}