CountDownLatch
public class CountDownLatchDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// 数量2表示:countDownLatch.countDown()调用2次才执行await
final CountDownLatch countDownLatch = new CountDownLatch(2);
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("t1线程进入初始化");
try {
countDownLatch.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("t1线程执行完毕");
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("t2线程进入初始化");
try {
Thread.sleep(3000);
countDownLatch.countDown();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("t2线程通知t1线程");
}
});
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("t3线程进入初始化");
try {
Thread.sleep(4000);
countDownLatch.countDown();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("t3线程通知t1线程");
}
});
t1.start();
t2.start();
t3.start();
}
输出结果:
t1线程进入初始化
t3线程进入初始化
t2线程进入初始化
t2线程通知t1线程
t3线程通知t1线程
t1线程执行完毕
CyclicBarrier
CyclicBarrier 应用场景,例子比如运动员起跑,必须等待所有运动员准备好了才能起跑。Demo
public class CyclicBarrierDemo {
static class Runner implements Runnable{
CyclicBarrier barrier;
String name;
public Runner(CyclicBarrier barrier,String name){
this.barrier = barrier;
this.name = name;
}
public void run() {
try {
Thread.sleep(new Random().nextInt(5) * 1000);
System.out.println(name+"准备好了");
barrier.await();
} catch (Exception e) {
// TODO: handle exception
}
System.out.println(name+"GO");
};
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CyclicBarrier barrier = new CyclicBarrier(3);
ExecutorService executor = Executors.newFixedThreadPool(3);
executor.submit(new Thread(new Runner(barrier, "张三")));
executor.submit(new Thread(new Runner(barrier, "李四")));
executor.submit(new Thread(new Runner(barrier, "王五")));
executor.shutdown();
}
}
输出结果:
张三准备好了
李四准备好了
王五准备好了
王五GO
张三GO
李四GO
重入锁ReentrantLock
这个Lock和synchronized 功能基本类似,区别在于,Lock 用法更为灵活一点。
public class ReentrantLockDemo {
ReentrantLock lock = new ReentrantLock();
Condition condition = lock.newCondition();
public void method1(){
try {
lock.lock();
System.out.println("进入method1");
condition.await();
System.out.println("退出method1");
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
}finally
{
lock.unlock();
}
}
public void method2(){
try {
lock.lock();
System.out.println("进入method2");
Thread.sleep(1000);
System.out.println("退出method2");
condition.signal();
} catch (Exception e) {
// TODO: handle exception
}finally
{
lock.unlock();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final ReentrantLockDemo lockDemo = new ReentrantLockDemo();
// new Thread(new Runnable() {
//
// @Override
// public void run() {
// // TODO Auto-generated method stub
// lockDemo.method1();
// lockDemo.method2();
// }
// }).start();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
lockDemo.method1();
}
},"t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
lockDemo.method2();
}
},"t2");
t1.start();
t2.start();
}
}
输出结果:
进入method1
进入method2
退出method2
退出method1
读写锁ReentrantReadWriteLock
读写锁用于读多写少,读读共享,读写互斥,写写互斥。
public class ReentrantReadAndWriteLockDemo {
ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
ReadLock readLock = rwLock.readLock();
WriteLock writeLock = rwLock.writeLock();
public void read(){
try {
readLock.lock();
System.out.println(Thread.currentThread().getName() + "调用了read");
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
}finally{
readLock.unlock();
}
}
public void write(){
try {
writeLock.lock();
System.out.println(Thread.currentThread().getName() + "调用了write");
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
}finally{
writeLock.unlock();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final ReentrantReadAndWriteLockDemo urw = new ReentrantReadAndWriteLockDemo();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
urw.read();
}
},"t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
urw.read();
}
},"t2");
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
urw.write();
}
},"t3");
t1.start();
// t2.start();
t3.start();
}
}