JUC包下的常见类:
1.ReenTrantLock(可重入锁):
java.util.concurrent.lock 中的 Lock 框架是锁定的一个抽象,它允许把锁定的实现作为 Java 类,而不是作为语言的特性来实现。这就为 Lock 的多种实现留下了空间,各种实现可能有不同的调度算法、性能特性或者锁定语义。 ReentrantLock 类实现了 Lock ,它拥有与 synchronized相同的并发性和内存语义,但是添加了类似锁投票、定时锁等候和可中断锁等候的一些特性。此外,它还提供了在激烈争用情况下更佳的性能。(换句话说,当许多线程都想访问共享资源时,JVM可以花更少的时候来调度线程,把更多时间用在执行线程上。)
public class TestDemo1 {
public static void main(String[] args) {
Lock lock = new ReentrantLock(true);
Runnable runnable = new Runnable() {
@Override
public void run() {
for (char item:"ABCD".toCharArray()){
lock.lock();
try {
System.out.print(item);
}finally {
lock.unlock();
}
}
}
};
Thread t1 = new Thread(runnable);
Thread t2 = new Thread(runnable);
t1.start();
t2.start();
}
}
2.semaphore(信号量):
可以用于限流一类的操作,semaphore其中的semaphore.release();semaphore.acquire();
public class TestDemo6 {
public static void main(String[] args) {
Semaphore semaphore = new Semaphore(2);
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10,10,0, TimeUnit.SECONDS
,new LinkedBlockingQueue<>(1000));
for (int i = 1; i <5 ; i++) {
int finali= i;
threadPoolExecutor.submit(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"到达停车场");
try {
Thread.sleep(1000);
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"得到车位");
try {
Thread.sleep(finali*500);
} catch (InterruptedException e) {
e.printStackTrace();
}
semaphore.release();
System.out.println(Thread.currentThread().getName()+"离开了停车场");
}
});
}
}
}
3.CountDownLatch-计数器
当全部线程进行到特定的位置后,才执行下一次的操作-只能使用一次 创建时后一个计数器,每次的CountDownLatch没让计数器减1,直到为0,执行下一步操作 CountDownLatch.await()和CountDownLatch.countdown
public class TestDemo3 {
public static void main(String[] args) throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(3);
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10,10,0,
TimeUnit.SECONDS,new LinkedBlockingQueue<>(1000));
threadPoolExecutor.submit(new Runnable() {
@Override
public void run() {
int i= new Random().nextInt(5);
i+= 1;
System.out.println(Thread.currentThread().getName()+"开跑");
try {
Thread.sleep(i*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"到达终点");
countDownLatch.countDown();
}
});
threadPoolExecutor.submit(new Runnable() {
@Override
public void run() {
int i= new Random().nextInt(5);
i+= 1;
System.out.println(Thread.currentThread().getName()+"开跑");
try {
Thread.sleep(i*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"到达终点");
countDownLatch.countDown();
}
});
threadPoolExecutor.submit(new Runnable() {
@Override
public void run() {
int i= new Random().nextInt(5);
i+= 1;
System.out.println(Thread.currentThread().getName()+"开跑");
try {
Thread.sleep(i*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"到达终点");
countDownLatch.countDown();
}
});
countDownLatch.await();
System.out.println("宣布成绩");
}
}
4.CyclicBarrier-循环屏障
和CountDownLatch的作用一样,只不过CountDownLatch只能使用一次,使用之后就会失效 CyclicBarrier可以无限使用,故称为循环屏障
public class TestDemo5 {
public static void main(String[] args) {
CyclicBarrier cyclicBarrier = new CyclicBarrier(2, new Runnable() {
@Override
public void run() {
System.out.println("-------到达了屏障------");
}
});
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10,10,0, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(1000));
for (int i = 1; i < 5; i++) {
int finalI = i;
threadPoolExecutor.submit(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(finalI*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"到达");
try {
cyclicBarrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
});
}
}
}
|