欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

JAVAJUC的整理

时间:2023-07-06

1.JAVA JUC简介

2.volatile关键字-内存可见性(比同步锁快)

public class TestVolatile {    public static void main(String[] args) {        ThreadDemo td=new ThreadDemo();        new Thread(td).start();              while (true) {                if (td.isFlag()) {                    System.out.println("-------");                    break;                }            }    }}class ThreadDemo implements Runnable {    private volatile boolean flag = false;    //private boolean flag = false;    @Override    public void run() {        try {            Thread.sleep(200);        } catch (InterruptedException e) {        }        flag = true;        System.out.println("flag="+ isFlag());    }    public boolean isFlag() {        return flag;    }    public void setFlag(boolean flag) {        this.flag = flag;    }}//输出结果为:-------            flag=true

3.原子变量-CAS算法

public class TestAtomicDemo {    public static void main(String[] args) {        AtomicDemo ad = new AtomicDemo();        for (int  i = 0; i < 10;i++) {            new Thread(ad).start();        }    }}class AtomicDemo implements Runnable {    //private volatile int serialNumber = 0;    private AtomicInteger serialNumber=new AtomicInteger();    @Override    public void run() {        try {            Thread.sleep(200);        } catch (InterruptedException e) {        }        System.out.println(Thread.currentThread().getName() + ":"+getSerialNumber());    }    public int getSerialNumber() {        return serialNumber.getAndIncrement();    }}

4.ConcurrentHashMap锁分段机制

5.CountDownLatch闭锁

6.实现Callable接口

7.Lock同步锁

8.Condition控制线程通信

9.线程八锁

10.线程按序交替

11.ReadWriteLock读写锁

12.线程池

13.线程调度

14.ForkJoinPool分支/合并框架 工作窃取

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。