package org.dance.tools;
import java.util.concurrent.TimeUnit;
/**
* 类说明:线程休眠辅助工具类
*/
public class SleepTools {
/**
* 按秒休眠
* @param seconds 秒数
*/
public static final void second(int seconds) {
try {
TimeUnit.SECONDS.sleep(seconds);
} catch (InterruptedException e) {
}
}
/**
* 按毫秒数休眠
* @param seconds 毫秒数
*/
public static final void ms(int seconds) {
try {
TimeUnit.MILLISECONDS.sleep(seconds);
} catch (InterruptedException e) {
}
}
}
package org.dance.day2.util;
import org.dance.tools.SleepTools;
import java.util.concurrent.CountDownLatch;
/**
* CountDownLatch的使用,有五个线程,6个扣除点
* 扣除完成后主线程和业务线程,才能执行工作
* 扣除点一般都是大于等于需要初始化的线程的
* @author ZYGisComputer
*/
public class UseCountDownLatch {
/**
* 设置为6个扣除点
*/
static CountDownLatch countDownLatch = new CountDownLatch(6);
/**
* 初始化线程
*/
private static class InitThread implements Runnable {
@Override
public void run() {
System.out.println("thread_" + Thread.currentThread().getId() + " ready init work .....");
// 执行扣减 扣减不代表结束
countDownLatch.countDown();
for (int i = 0; i < 2; i++) {
System.out.println("thread_" + Thread.currentThread().getId() + ".....continue do its work");
}
}
}
/**
* 业务线程
*/
private static class BusiThread implements Runnable {
@Override
public void run() {
// 业务线程需要在等初始化完毕后才能执行
try {
countDownLatch.await();
for (int i = 0; i < 3; i++) {
System.out.println("BusiThread " + Thread.currentThread().getId() + " do business-----");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// 创建单独的初始化线程
new Thread(){
@Override
public void run() {
SleepTools.ms(1);
System.out.println("thread_" + Thread.currentThread().getId() + " ready init work step 1st.....");
// 扣减一次
countDownLatch.countDown();
System.out.println("begin stop 2nd.....");
SleepTools.ms(1);
System.out.println("thread_" + Thread.currentThread().getId() + " ready init work step 2nd.....");
// 扣减一次
countDownLatch.countDown();
}
}.start();
// 启动业务线程
new Thread(new BusiThread()).start();
// 启动初始化线程
for (int i = 0; i <= 3; i++) {
new Thread(new InitThread()).start();
}
// 主线程进入等待
try {
countDownLatch.await();
System.out.println("Main do ites work.....");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
返回结果:
thread_13 ready init work .....
thread_13.....continue do its work
thread_13.....continue do its work
thread_14 ready init work .....
thread_14.....continue do its work
thread_14.....continue do its work
thread_15 ready init work .....
thread_15.....continue do its work
thread_11 ready init work step 1st.....
begin stop 2nd.....
thread_16 ready init work .....
thread_16.....continue do its work
thread_16.....continue do its work
thread_15.....continue do its work
thread_11 ready init work step 2nd.....
Main do ites work.....
BusiThread 12 do business-----
BusiThread 12 do business-----
BusiThread 12 do business-----
通过返回结果就可以很直接的看到业务线程是在初始化线程完全跑完之后,才开始执行的