多线程闭锁
@Slf4j
public class CountDownLatchTest {
private static final int poolSize = 4;
private static final ThreadFactory THREAD_FACTORY = new ThreadFactoryBuilder().setNameFormat("ThreadPoolContainer-thread-%d").build();
public static void main(String[] args) {
ArrayList<String> demoList = Lists.newArrayList();
demoList.add("111");
demoList.add("222");
demoList.add("333");
List<List<String>> lists = Lists.partition(demoList, 1);
ThreadPoolExecutor executor = new ThreadPoolExecutor(poolSize, poolSize, 2, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(1024), THREAD_FACTORY, new ThreadPoolExecutor.AbortPolicy());
CountDownLatch latch = new CountDownLatch(lists.size());
List<FutureTask<Boolean>> taskList = new ArrayList<>();
for (List<String> list : lists) {
FutureTask<Boolean> task = new FutureTask<>(new Callable<Boolean>() {
@Override
public Boolean call() {
Boolean result = false;
try {
System.out.println(list + Thread.currentThread().getName());
result = true;
} finally {
latch.countDown();
}
return result;
}
});
executor.submit(task);
taskList.add(task);
}
try {
boolean allFinished = latch.await(10, TimeUnit.SECONDS);
if (!allFinished) {
log.error("多线程闭锁等待了10s,仍未执行完毕");
}
} catch (InterruptedException e) {
log.error("线程中断!", e);
Thread.currentThread().interrupt();
}
for (FutureTask<Boolean> task : taskList) {
Boolean result = false;
try {
result = task.get();
System.out.println(result + "-" + task);
} catch (Exception e) {
log.error("线程中断!", e);
Thread.currentThread().interrupt();
}
if(!result){
System.out.println("多线程闭锁存在某线程处理异常!");
}
}
executor.shutdown();
}
}
多线程栅栏
@Slf4j
public class CyclicBarrierTest {
private static final int poolSize = 5;
private static final ThreadFactory THREAD_FACTORY = new ThreadFactoryBuilder().setNameFormat("ThreadPoolContainer-thread-%d").build();
public static void main(String[] args) {
ArrayList<String> demoList = Lists.newArrayList();
demoList.add("111");
demoList.add("222");
demoList.add("333");
demoList.add("444");
demoList.add("555");
List<List<String>> lists = Lists.partition(demoList, 1);
ThreadPoolExecutor executor = new ThreadPoolExecutor(poolSize, poolSize, 5, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(1024), THREAD_FACTORY, new ThreadPoolExecutor.AbortPolicy());
CyclicBarrier cyclicBarrier = new CyclicBarrier(lists.size(), new Runnable() {
@Override
public void run() {
System.out.println("这个线程打个叉!");
}
});
for (List<String> list : lists) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(list + Thread.currentThread().getName());
try {
cyclicBarrier.await();
System.out.println(Thread.currentThread().getName() + "接着执行线程");
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
}
});
executor.submit(thread);
}
executor.shutdown();
}
}
多线程信号量
@Slf4j
public class SemaphoreTest {
private static final int poolSize = 5;
private static final ThreadFactory THREAD_FACTORY = new ThreadFactoryBuilder().setNameFormat("ThreadPoolContainer-thread-%d").build();
public static void main(String[] args) {
ArrayList<String> demoList = Lists.newArrayList();
demoList.add("111");
demoList.add("222");
demoList.add("333");
demoList.add("444");
demoList.add("555");
demoList.add("666");
List<List<String>> lists = Lists.partition(demoList, 1);
ThreadPoolExecutor executor = new ThreadPoolExecutor(poolSize, poolSize, 5, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(1024), THREAD_FACTORY, new ThreadPoolExecutor.AbortPolicy());
Semaphore semaphore = new Semaphore(3);
for (List<String> list : lists) {
Thread task = new Thread(new Runnable() {
@Override
public void run() {
try {
semaphore.acquire();
System.out.println(Thread.currentThread().getName() + "抢到资源");
TimeUnit.SECONDS.sleep(3);
System.out.println(Thread.currentThread().getName() + "放开资源");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
semaphore.release();
}
}
});
executor.submit(task);
}
executor.shutdown();
}
}