JUC下的几种线程计数器以及读写锁
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JUC下的几种线程计数器以及读写锁相关的知识,希望对你有一定的参考价值。
1-CountDownLanch--JUC
package thread20200414;
import java.util.concurrent.CountDownLatch;
/**
CountDownLaunch做线程减法,即所有线程执行完后,才可以进行下一步
一个线程等待其他线程执行完毕后,才执行
* @author zhaomin
* @date 2020/4/15 8:35
*/
public class CountDownLanchTest {
public static void main(String[] args) {
CountDownLatch countDownLatch=new CountDownLatch(6);
for(int i=0;i<6;i++){
new Thread(()->{
System.out.println(Thread.currentThread().getName()+"--离开教室");
countDownLatch.countDown();
},String.valueOf(i)).start();
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("main离开教室");
}
}
2- CyclicBarrier--JUC
package thread20200414;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
/**
* 线程加法,只有等到一定数量的线程都开启后,才进行下一步(执行runnable里的方法)
* @author zhaomin
* @date 2020/4/15 8:51
*/
public class CyclicBarrierTest {
public static void main(String[] args) {
Runnable runnable=new Runnable() {
@Override
public void run() {
System.out.println("集齐7颗龙珠,召唤神龙");
}
};
CyclicBarrier cyclicBarrier=new CyclicBarrier(7,runnable);
for(int i=0;i<7;i++){
final int count=i;//Lambda表达式中的变量必须是final的
new Thread(()->{
System.out.println(count+"颗龙珠");
try {
cyclicBarrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
},String.valueOf(i)).start();
}
}
}
3-Semaphore--JUC
package thread20200414;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
/**
* 抢车位,狼多肉少-3个资源6和线程
* @author zhaomin
* @date 2020/4/15 15:06
*/
public class SemaphoreTest {
public static void main(String[] args) {
Semaphore semaphore=new Semaphore(3);
for(int i=1;i<=6;i++){
new Thread(()->{
try {
semaphore.acquire();//获取到了资源,就让资源数减一
System.out.println(Thread.currentThread().getName()+"抢占车位");
TimeUnit.SECONDS.sleep(3);
System.out.println(Thread.currentThread().getName()+"离开车位");
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
semaphore.release();//释放资源就让资源加一
}
},String.valueOf(i)).start();
}
}
}
4-读写锁
读读可共享--写读、写写要独占
package thread20200414;
import com.sun.org.apache.xpath.internal.SourceTree;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* 读写锁
* 读读可同时
* 读写、写写要独占
* @author zhaomin
* @date 2020/4/15 16:10
*/
class Book{
private volatile Map<String,String> map=new HashMap<>();
ReadWriteLock readWriteLock=new ReentrantReadWriteLock();
public void put(String key, String val) {
readWriteLock.writeLock().lock();//加写锁
try{
System.out.println("正在写---");
map.put(key,val);
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"写入-"+key);
System.out.println("写完了---");
}finally {
readWriteLock.writeLock().unlock();
}
}
public void get(String key){
readWriteLock.readLock().lock();//加读锁
try{
System.out.println("读书中---");
String s = map.get(key);
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"读取-"+s);
System.out.println("读完了---");
}finally {
readWriteLock.readLock().unlock();
}
}
}
public class ReadWriteLockTest {
public static void main(String[] args) {
Book book=new Book();
for(int i=1;i<5;i++) {
final int tem=i;
new Thread(()->{
book.put(String.valueOf(tem),String.valueOf(tem));
},String.valueOf(i)).start();
}
for (int i=1;i<=5;i++) {
final int tem=i;
new Thread(()->{
book.get(String.valueOf(tem));
},String.valueOf(i)).start();
}
}
}
以上是关于JUC下的几种线程计数器以及读写锁的主要内容,如果未能解决你的问题,请参考以下文章
JUC并发编程 共享模式之工具 JUC CountdownLatch(倒计时锁) -- CountdownLatch应用(等待多个线程准备完毕( 可以覆盖上次的打印内)等待多个远程调用结束)(代码片段