List<Integer> 1 = new ArrayList<>();
List<Integer> l = new Vector<>(); // 同步sync
List<Integer> l = Collections.synchronizedList(new ArrayList<>()); // 同步sync
List<Integer> l = new CopyOnWriteArrayList<>(); // Lock锁 JUC里面的
Set<Integer> s = new HashSet<>(); // 就是hashmap的键
Set<Integer> s= Collections.synchronizedSet(new HashSet<>());// 同步sync
Set<Integer> s= new CopyOnWriteArraySet<>();// Lock 锁 JUC里面的
// -----------------------------
Map<String,String> map = new HashMap<String,String>(16,0.75f);
Map<String,String> map2 = new Hashtable<>();// 同步sync
Map<String,String> map = Collections.synchronizedMap(new HashMap<>());// 同步sync
Map<String,String> map = new ConcurrentHashMap<>();//分段Lock锁,锁定某一段 JUC里面的
//BlockingQueue 实现类都是用了Lock的,即线程安全的
BlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(10);
BlockingQueue<Integer> q = new LinkedBlockingDeque<Integer>();
// 同步队列,只能存放一个元素
BlockingQueue<Integer> q = new SynchronousQueue<Integer>();
BlockingQueue<Integer> q = (BlockingQueue<Integer>) new ConcurrentLinkedQueue<Integer>();
// 本质就是ThreadPoolExecutor(7各参数)
Executors.newSingleThreadExecutor();
Executors.newFixedThreadPool(5);
Executors.newCachedThreadPool();
// 推荐手动创建
ExecutorService s = new ThreadPoolExecutor(
2, // 一直开着的上数量
Runtime.getRuntime().availableProcessors(),// 最大线程数,一般就是CUP核心数
3, // 等待
TimeUnit.SECONDS, // 单位
new LinkedBlockingDeque<>(3),// 等待队列 最多同时开启max+cap个线程
Executors.defaultThreadFactory(), // 默认线程工厂
// 拒绝策略,有四种
//new ThreadPoolExecutor.AbortPolicy() //线程超过max+cap,又来任务,抛出异常
//new ThreadPoolExecutor.CallerRunsPolicy() //线程超过max+cap,线程池不受理,来源线程执行
//new ThreadPoolExecutor.DiscardPolicy() //线程超过max+cap,又来任务,不抛出异常,但丢弃任务
new ThreadPoolExecutor.DiscardOldestPolicy() //线程超过max+cap,又来任务,尝试与第一个任务竞争,竞争失败,不抛异常,但还丢弃任务
List<String> l = new ArrayList<>();
l.stream()
.filter(str->{return str.length()>=2;}) // 断定型
.map(str->{return str.toLowerCase();}) // 转换型
.sorted((str1,str2)->str1.compareTo(str2)) // 排序
.limit(1) // 只输出1个
.forEach(System.out::println);
class ForkJoinDemo extends RecursiveTask<Long>{
private Long start;
private Long end;
private Long temp=100000L; // 临界值
public ForkJoinDemo(){
}
public ForkJoinDemo(Long start, Long end){
this.start = start;
this.end = end;
}
@Override
protected Long compute() {
if(end-start < temp){
Long sum = 0L;
for(Long i = start; i <= end; i++){
sum += i;
}
return sum;
}else{
Long mid = (start + end)/2;
ForkJoinDemo f1 = new ForkJoinDemo(start,mid);
f1.fork(); //任务压入队列
ForkJoinDemo f2 = new ForkJoinDemo(mid+1,end);
f2.fork();
return f1.join()+f2.join();
}
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
// 1 加到10亿
// 方法一 for循环累加
// 方法二
ForkJoinPool pool = new ForkJoinPool();
ForkJoinTask<Long> submit = pool.submit(new ForkJoinDemo(1L,10_0000_0000L));// 提交任务
System.out.println(submit.get());
// 方法三 最快
LongStream.range(0L,10_0000_0000L).parallel().reduce(0,Long::sum);
}
}
public static void main(String args[]) throws ExecutionException, InterruptedException {
CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(()->{
System.out.println("com1执行");// 耗时操作
});
System.out.println(completableFuture.get()); // null
CompletableFuture<Integer> completableFuture2 = CompletableFuture.supplyAsync(()->{
System.out.println("com2执行");
return 2; // 有返回值
});
completableFuture2.whenComplete((t,u)->{
System.out.println("t=>"+t); // 正常执行的返回结果
System.out.println("u=>"+u); // 错误信息
}).exceptionally((e)->{
System.out.println(e.getMessage());//
return 500; // 出错了返回的结果
});
System.out.println(completableFuture2.get());
}
不存在的东西,是一种约定
程序不知道主存中的值被修改了。
https://blog.dean0731.top/post/8
AtomicInteger num = new AtomicInteger(2020);
// 比较并交换 cup的并发原语,即能直接操作内存
num.compareAndSet(2020,2021);
System.out.println(num.get());
num.getAndIncrement();
带版本号的原子操作
CAS实现自旋锁
class SpinLock{
AtomicReference<Thread> f = new AtomicReference<>();
public void lock(){
// f 可以说就是当前线程, f为空, f就变为当前线程, 其他线程来拿锁的时候不为空,一直循环
while(!f.compareAndSet(null,Thread.currentThread())){
}
}
public void unlock(){
// 释放锁,
f.compareAndSet(Thread.currentThread(),null);
}
}
排查死锁
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有