java并发工具类
CountDownLatch
- 可以理解成维护的是一个计数器。一般用于某个线程A等待若干个其他线程执行完任务之后,它才执行。强调等待一个线程等待多个线程
CyclicBarrier
- 可以理解成维护的就是一个计数器,一般用于一组线程相互等待至某个状态,这一组线程再继续进行。
Semaphore
- Semaphore(信号量)是用于控制 同时访问特定资源的线程的数量,它通过协调各个线程,保证合理的使用公共资源。
线程可以通过acquire()方法来获取信号量的许可,当信号量中没有可用的许可的时候,线程阻塞,直到有可用的许可为止。线程可以通过release()方法释放它持有的信号量的许可。
exchanger
- 是一个用于线程间协作的工具类。Exchanger用于进行线程间的数据交换。它提供一个同步点,在这个同步点两个线程可以交换彼此的数据。这两个线程通过exchange方法交换数据, 如果第一个线程先执行exchange方法,它会一直等待第二个线程也执行exchange,当两个线程都到达同步点时,这两个线程就可以交换数据,将本线程生产出来的数据传递给对方。因此使用Exchanger的重点是成对的线程使用exchange()方法,当有一对线程达到了同步点,就会进行交换数据。因此该工具类的线程对象是成对的。
上代码~
import java.util.concurrent.CountDownLatch;
public class CountDownLatchTest {
private final static CountDownLatch countDownLatch = new CountDownLatch(5);
public static void main(String[] args) throws InterruptedException {
new SimpleThread().start();
new WorkThread("workThread1").start();
new WorkThread("workThread2").start();
new WorkThread("workThread3").start();
}
private static class WorkThread extends Thread{
private String name;
public WorkThread(String name){
this.name = name;
}
public void run(){
System.out.println("workThread name: " + name);
countDownLatch.countDown();
}
}
private static class SimpleThread extends Thread{
public void run(){
System.out.println("simple thread"+Thread.currentThread().getName()+" started");
try{
countDownLatch.await();
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println("simple thread"+Thread.currentThread().getName()+" ended");
}
}
}
import java.util.concurrent.*;
public class CyclicBarrierTest {
public static void main(String[] args) {
CyclicBarrier cb = new CyclicBarrier(3,()->{
System.out.println("Tasks准备完毕!");
});
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
4,
8,
0,
TimeUnit.SECONDS,
new LinkedBlockingQueue<>()
);
for (int i = 0; i < 3; i++) {
threadPoolExecutor.execute(new Task(i, cb));
}
System.out.println("main线程任务分配完毕");
}
public static class Task extends Thread{
private int id;
private CyclicBarrier cb;
public Task(int i, CyclicBarrier cb){
this.id = i;
this.cb = cb;
}
public void run(){
try {
System.out.println("thread "+id+"is ready!");
cb.await();
System.out.println("thread "+id+"is going on!");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
}
}
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class SemaphoreTest {
private final static int PERMITS = 5;
private volatile static int var = 0;
public static void main(String[] args) {
ExecutorService es = Executors.newCachedThreadPool();
final Semaphore semaphore = new Semaphore(5);
for (int i = 0; i < 10; i++) {
es.submit(()->{
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
semaphore.acquire();
System.out.println("var" + var++);
semaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
}
}
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Exchanger;
public class ExchangerTest {
public static void main(String[] args) {
Exchanger<List<String>> exchanger = new Exchanger<>();
new Thread(()->{
List<String> list = new LinkedList<>();
list.add("hi");
list.add("hello");
list.add("hello2");
list.add("hello");
try {
exchanger.exchange(list);
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(()->{
List<String> list = new LinkedList<>();
try {
list = exchanger.exchange(list);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (String s: list
) {
System.out.println(s);
}
}).start();
}
}