t update):如果当前值等于预期值,则以原子方式将数组位置i的元素设置成update值。
AtomicIntegerArray的两个构造方法:
AtomicIntegerArray(int length):指定数组的大小,并初始化为0
AtomicIntegerArray(int [] array):对给定的数组进行拷贝。
案例:
int value[] = new int[]{1,2,3};
AtomicIntegerArray aia = new AtomicIntegerArray(value);
System.out.println(aia.getAndSet(1, 9));
System.out.println(aia.get(1));
System.out.println(value[1]);
运行结果:2 9 2
43. CountDownLatch
CountDownLatch允许一个或多个线程等待其他线程完成操作。CountDownLatch的构造函数接收一个int类型的参数作为计数器,如果你想等待N个点完成,这里就传入N(CountDownLatch(int count))。
CountDownLatch的方法有:await(), await(long timeout, TimeUnit unit), countDown(), getCount()等。
计数器必须大于等于0,只是等于0的时候,计数器就是零,调用await方法时不会阻塞当前线程。CountDownLatch不可能重新初始化或者修改CountDownLatch对象的内部计数器的值。一个线程调用countDown方法happens-before另一个线程调用的await()方法。
44. CyclicBarrier
让一组线程达到一个屏障时被阻塞,知道最后一个线程到达屏障时,屏障才会开门,所有被屏障拦截的线程才会继续运行。CyclicBarrier默认的构造方法是CyclicBarrier(int parties),其参数表示屏障拦截的线程数量,每个线程调用await方法告诉CyclicBarrier我已经达到了屏障,然后当前线程被阻塞。CyclicBarrier还提供了一个更高级的构造函数CyclicBarrier(int parties, Runnable barrierAction)用于在线程达到屏障时,优先执行barrierAction,方便处理更复杂的业务场景,举例如下。
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class CyclicBarrierTest
{
static CyclicBarrier c = new CyclicBarrier(2,new A());
public static void main(String[] args)
{
new Thread(new Runnable(){
@Override
public void run()
{
try
{
System.out.println(1);
c.await();
}
catch (InterruptedException | BrokenBarrierException e)
{
e.printStackTrace();
}
System.out.println(2);
}
}).start();
try
{
System.out.println(3);
c.await();
}
catch (InterruptedException | BrokenBarrierException e)
{
e.printStackTrace();
}
System.out.println(4);
}
static class A implements Runnable
{
@Override
public void run()
{
System.out.println(5);
}
}
}
输出结果:3 1 5 2 4
45. CyclicBarrier和CountDownLatch的区别
CountDownLatch的计数器只能使用一次,而CyclicBarrier的计数器可以使用reset()方法重置。
46. Semaphore
Semaphore(信号量)是用来控制同事访问特定资源的线程数量,它协调各个线程,以保证合理的使用公共资源。Semaphore有两个构造函数:Semaphore(int permits)默认是非公平的,Semaphore(int permits, boolean fair)可以设置为公平的。应用案例如下:
public class SemaphoreTest
{
private static final int THREAD_COUNT=30;
private static ExecutorService threadPool = Executors.newFixedThreadPool(30);
private static Semaphore s = new Semaphore(10);
public static void main(String[] args)
{
for(int i=0;i<THREAD_COUNT;i++)
{
final int a = i;
threadPool.execute(new Runnable(){
@Override
public void run()
{
try
{
s.acquire();
System.out.println("do something...."+a);
s.release();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
});
}
threadPool.shutdown();
}
}
由上例可以看出Semaphore的用法非常的简单,首先线程使用Semaphore的acquire()方法获取一个许可证,使用完之后调用release()方法归还许可证。还可以用tryAcquire()方法尝试获取许可证。Semaphore还提供了一些其他方法: int availablePermits()返回此信号量中当前可用的许可证数;int getQueueLength()返回正在等待获取许可证的线程数;boolean hasQueuedThreads()是否有线程正在等待获取许可证;void reducePermits(int reduction)减少reduction个许可证,是个protected方法;Collection<Thread> getQueuedThreads()返回所有等待获取许可证的线程集合,也是一个protected方法。
47. 线程间交换数据的Exchanger
Exchanger是一个用于线程间协作的工具类。Exchanger用于进行线程间的数据交换。它提供一个同步点,在这个同步点,两个线程可以