countDownLatch
创建countDownLatch对象
每次线程调用countDown
在主线程设置await
每次调用countDown时,countDown对象计数器都会-1,当减到3时,await就会进行释放,主线程将会启动
import java.util.concurrent.*;
public class threadpooltest{
public static void main(String[] args) {
// CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
CountDownLatch countDownLatch = new CountDownLatch(3);
//创建线程池
ExecutorService executor = new ThreadPoolExecutor(5, 5, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1)) {
@Override protected void beforeExecute(Thread t, Runnable r) {
System.out.println("beforeExecute is called");
}
@Override protected void afterExecute(Runnable r, Throwable t) {
System.out.println("afterExecute is called");
}
@Override protected void terminated() {
System.out.println("terminated is called");
}
};
executor.submit(()->{
System.out.println("减一");
try {
countDownLatch.countDown();
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
});
executor.submit(()->{
System.out.println("减一");
try {
countDownLatch.countDown();
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
});
executor.submit(()->{
System.out.println("减一");
try {
countDownLatch.countDown();
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
});
try {
countDownLatch.await();
System.out.println("主线程启动");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
cyclicBarrier
cyclicBarrier的作用是在所有线程都准备完成后进行同一的运行
在线程准备完成后,统一运行
首先创建cyclicBarrier以及计数量3
每个线程准备完成后执行await
当三个线程都进行await操作后,三个线程将会同时往下执行
import java.util.concurrent.*;
public class threadpooltest{
public static void main(String[] args) {
CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
// CountDownLatch countDownLatch = new CountDownLatch(3);
//创建线程池
ExecutorService executor = new ThreadPoolExecutor(5, 5, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1)) {
@Override protected void beforeExecute(Thread t, Runnable r) {
System.out.println("beforeExecute is called");
}
@Override protected void afterExecute(Runnable r, Throwable t) {
System.out.println("afterExecute is called");
}
@Override protected void terminated() {
System.out.println("terminated is called");
}
};
executor.submit(()->{
System.out.println("进入准备");
try {
cyclicBarrier.await();
System.out.println("同时启动");
} catch (Exception e) {
e.printStackTrace();
}
});
executor.submit(()->{
System.out.println("进入准备");
try {
cyclicBarrier.await();
System.out.println("同时启动");
} catch (Exception e) {
e.printStackTrace();
}
});
executor.submit(()->{
System.out.println("进入准备");
try {
cyclicBarrier.await();
System.out.println("同时启动");
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
semaphore
semaphore是一组信号量,线程在使用acquire后会使semaphore信号量-1,若信号量为0,则线程阻塞。使用release释放信号量,semaphore信号量+1,此时若阻塞的线程获取到信号量则可继续执行
import java.util.concurrent.Semaphore;
public class SemaphoreTest {
public static void main(String[] args) {
//创建信号量为6的Semaphore对象
Semaphore semaphore = new Semaphore(6);
//创建50个线程获取信号量
for (int i = 0; i < 50; i++) {
new Thread(()->{
try {
//获取信号量,若信号量为0则阻塞
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
//释放获取的信号量
semaphore.release();
}
System.out.println("aaa");
}).start();
}
}
}