CountDownLatch
某个线程,等待其他线程执行完,再执行该线程。
方法摘要 |
---|
void | await() 使当前线程在锁存器倒计数至零之前一直等待,除非线程被中断。 |
boolean | await(long timeout, TimeUnit unit) 使当前线程在锁存器倒计数至零之前一直等待,除非线程被中断或超出了指定的等待时间。 |
void | countDown() 递减锁存器的计数,如果计数到达零,则释放所有等待的线程。 |
long | getCount() 返回当前计数。 |
String | toString() 返回标识此锁存器及其状态的字符串。 |
实现思路:
线程thread1:循环输出0~9, 线程thread2:循环输出10~19, 线程thread3:循环输出20~29 以上3个线程完成后,在执行mainThread线程
输出结果:
代码实现:
测试类
package countdownlatch.demo;
import java.util.concurrent.CountDownLatch;
/**
* @program: Demo1
* @description: 测试类
* @create: 2021-07-07 09:54
*/
public class test {
public static void main(String[] args) {
// 3:定义需要执行的完成的数量,执行完成后,main线程会自动执行
CountDownLatch latch = new CountDownLatch(3);
thread1 t1 = new thread1(latch);
t1.setName("Thread1");// 设置线程名称
thread2 t2 = new thread2(latch);
t2.setName("Thread2");// 设置线程名称
thread3 t3 = new thread3(latch);
t3.setName("Thread3");// 设置线程名称
mainThread maint = new mainThread(latch);
maint.setName("MainThread");// 设置线程名称
// 开启线程
maint.start();
t1.start();
t2.start();
t3.start();
}
}
thred1
package countdownlatch.demo;
import java.util.concurrent.CountDownLatch;
/**
* @program: Demo1
* @description: 线程1
* @create: 2021-07-07 09:54
*/
public class thread1 extends Thread{
private CountDownLatch latch = null;
public thread1(CountDownLatch latch) {
this.latch = latch;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(getName() + "执行代码" + i);
}
// 递减锁存器的计数
latch.countDown();
// 返回当前计数
System.out.println(getName()+"返回当前计数" + latch.getCount());
}
}
thread2
package countdownlatch.demo;
import java.util.concurrent.CountDownLatch;
/**
* @program: Demo1
* @description: 线程2
* @create: 2021-07-07 09:54
*/
public class thread2 extends Thread{
private CountDownLatch latch = null;
public thread2(CountDownLatch latch) {
this.latch = latch;
}
@Override
public void run() {
for (int i = 10; i < 20; i++) {
System.out.println(getName()+"执行代码"+i);
}
// 递减锁存器的计数
latch.countDown();
// 返回当前计数
System.out.println(getName()+"返回当前计数" + latch.getCount());
}
}
thread3
package countdownlatch.demo;
import java.util.concurrent.CountDownLatch;
/**
* @program: Demo1
* @description: 线程3
* @create: 2021-07-07 09:54
*/
public class thread3 extends Thread{
private CountDownLatch latch = null;
public thread3(CountDownLatch latch) {
this.latch = latch;
}
@Override
public void run() {
for (int i = 20; i < 30; i++) {
System.out.println(getName() + "执行代码" + i);
}
// 递减锁存器的计数
latch.countDown();
// 返回当前计数
System.out.println(getName()+"返回当前计数" + latch.getCount());
}
}
mainThread
package countdownlatch.demo;
import java.util.concurrent.CountDownLatch;
/**
* @program: Demo1
* @description: 等待其他线程执行,在执行线程 mainThread
* @author: Wu Yingying
* @create: 2021-07-07 10:00
*/
public class mainThread extends Thread {
private CountDownLatch latch = null;
public mainThread(CountDownLatch latch) {
this.latch = latch;
}
@Override
public void run() {
try {
// 使当前线程在锁存器倒计数至零之前一直等待,除非线程被中断或超出了指定的等待时间。
latch.await();
System.out.println(getName()+"_3个线程都执行完了,该我上场执行了,哈哈哈");
} catch (InterruptedException e) {
e.printStackTrace();
// 返回当前计数
System.out.println(getName()+"返回当前计数" + latch.getCount());
}
}
}