各种同步控制工具的使用

Semaphore
概述

主要接口

使用

public class SemaphoreDemo {
private static final int THREAD_COUNT = 3;
private static ExecutorService threadPool = Executors
.newFixedThreadPool(THREAD_COUNT);

private static Semaphore s = new Semaphore(1);
public static void main(String[] args) {
for(int i=0;i<3;i++)
{
threadPool.execute(new Runnable() {
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName()+"start data");
Thread.sleep(2000);
s.acquire();
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName()+"save data");
s.release();
System.out.println(Thread.currentThread().getName()+"release data");
Thread.sleep(2000);
System.out.println(Thread.currentThread().getName()+"end data");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
threadPool.shutdown();
}
}

最后得出一个比较有意思的结论:Semaphore 像是一个共享的屋子,这个屋子里面只能有一定的人数,这个人数是所有人可以看到的,甚至与release()这个方法,可以被别的线程进行调用,

一般使用acquire() 与release() 这个之间的代码只能有固定数量的线程存在,当然这种是当前线程进行获取和释放

ReadWriteLock
概述

主要使用

public void run() {
//isRead自定义变量(判断这个线程是读还是写)
if (isRead) {
//获取读锁
myLock.readLock().lock();
System.out.println("读");
//释放读锁
myLock.readLock().unlock();
} else {
//获取写锁
myLock.writeLock().lock();
//执行现金业务
System.out.println("写");
//释放写锁
myLock.writeLock().unlock();
}
}

CountDownLatch
概述:

public class CountDownLatchDemo {
private static CountDownLatch countDownLatch=new CountDownLatch(10);
public static void main(String[] args) {
for(int i=0;i<10;i++)
{
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"work");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
countDownLatch.countDown();

}
}).start();
}
new Thread(new Runnable() {
@Override
public void run() {
try {
countDownLatch.await();
System.out.println(Thread.currentThread().getName()+"主线程start");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"main1").start();
}
}

CyclicBarrier
概述

public class CyclicBarrierDemo {
public static class Soldier implements Runnable{
private String name;
private final CyclicBarrier cyclicBarrier;

public Soldier(String name,CyclicBarrier c) {
this.name = name;
this.cyclicBarrier=c;
}

@Override
public void run() {
try{
//等待所有士兵到齐
System.out.println(name +"报道");
cyclicBarrier.await();
dowork();
//等待所有士兵完成工作
cyclicBarrier.await();

}
catch (Exception e)
{
e.printStackTrace();
}
}
public void dowork()
{
System.out.println(name +"完成任务");
}
}

public static class BarrierRun implements Runnable{
boolean flag;
int number;
public BarrierRun(boolean flag, int number) {
this.flag = flag;
this.number = number;
}

@Override
public void run() {
if(!flag)
{
System.out.println("士兵集合完毕");
flag=true;
System.out.println("开始执行任务");
}
else{
System.out.println("任务完成");

}
}
}
public static void main(String[] args) {
final int N =10;

CyclicBarrier barrier =new CyclicBarrier(N,new BarrierRun(false,N));
System.out.println("集合队伍");
for(int i=0;i<n;i++)
{
new Thread(new Soldier("士兵"+i,barrier)).start();
}
}
}

每次CyclicBarrier 调用await()方法之后,都会等待所有的子线程,之后执行CyclicBarrier 的Runnable的方法

LockSupport
概述

park和unpark的灵活之处

</n;i++)</10;i++)</3;i++)