并发包java.util.concurrent学习(一)

一:CountDownLatch用法

二:CyclicBarrier用法

三:Semaphore用法


一:CountDownLatch类:

官方解释:A synchronization aid that allows one or more threads to wait until. a set of operations being performed in other threads completes.(允许一个或多个线程等待的同步辅助工具。在其他线程中执行的一组操作完成。)

package com.sample.concurrent.count;

import java.util.concurrent.CountDownLatch;

public class CountDownLatchTest {

	public static CountDownLatch cdl = new CountDownLatch(10);

	public static void main(String[] args) throws InterruptedException {

		while (cdl.getCount() > 0) {
			new Thread(new Runnable() {
				@Override
				public void run() {
					System.out.println("准备发射导弹:Thread-" + Thread.currentThread().getName());
					try {
						//等待CountDownLatch的count为零,阻塞线程
						cdl.await();
						System.out.println("开始发射导弹:Thread-" + Thread.currentThread().getName());
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}

			}).start();
			Thread.sleep(500);
			cdl.countDown();
		}
	}

}

二:CyclicBarrier类:

官方解释:A synchronization aid that allows a set of threads to all wait for  each other to reach a common barrier point.  CyclicBarriers are useful in programs involving a fixed sized party of threads that must occasionally wait for each other. The barrier is called cyclic because it can be re-used after the waiting threads are released.(允许一组线程全部等待彼此达到共同await点的同步辅助工具。 循环阻塞在涉及固定大小的线程数的程序中很有用,这些线程必须等待彼此全部执行到await状态。 阻塞被称为循环 ,因为它可以在等待的线程被释放之后重新使用。)

package com.sample.concurrent.count;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierTest {

	public static void main(String[] args) throws InterruptedException {
		CyclicBarrier cb = new CyclicBarrier(10, new Runnable() {
			@Override
			public void run() {
				System.out.println("所有线程执行完毕!");
			}
		});
		
		for(int i = 0; i < cb.getParties(); i ++){
			new Thread(new Runnable(){
				@Override
				public void run() {
					System.out.println("start-thread-" + Thread.currentThread().getName());
					try {
						Thread.sleep(500);
						System.out.println("end-thread-" + Thread.currentThread().getName());
						/**调用await,阻塞线程, 当阻塞线程数量等于CyclicBarrier的parties参数时,则线程不再阻塞
						    当没有线程阻塞的时候,执行CyclicBarrier的Runnable实现类barrierAction**/
						cb.await();
					} catch (InterruptedException | BrokenBarrierException e) {
						e.printStackTrace();
					}
				}
			}).start();;
		}
	}
}

三:Semaphore类:

官方解释:A counting semaphore.  Conceptually, a semaphore maintains a set of permits.  Each {@link #acquire} blocks if necessary until a permit is available, and then takes it.  Each {@link #release} adds a permit, potentially releasing a blocking acquirer. However, no actual permit objects are used; the {@code Semaphore} just keeps a count of the number available and acts accordingly.(一个计数信号量。 在概念上,信号量维持一组许可证。 如果有必要(又不需要阻塞的,tryAcquire()),每个acquire()都会阻塞,直到许可证可用,然后才能使用它。 每个release()释放许可证,潜在地释放阻塞获取方。 但是,没有使用实际的许可证对象; Semaphore只保留可用数量的计数,并相应地执行。)

package com.sample.concurrent.count;


import java.util.concurrent.Semaphore;


public class SemaphoreTest {
public static void main(String[] args) throws InterruptedException {
// 公共厕所一共有三个马桶
int n = 3;
Semaphore sem = new Semaphore(n);
for (int i = 0; i < n + 3; i++) {
Thread.sleep(500);
new Thread(new Runnable(){


@Override
public void run() {
try {
sem.acquire();
System.out.println("开始蹲坑:Thread-" + Thread.currentThread().getName());
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
sem.release();
}
System.out.println("结束蹲坑:Thread-" + Thread.currentThread().getName());
}

}).start();
}
}
}

猜你喜欢

转载自blog.csdn.net/qq_33820379/article/details/80852165