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分支/合并框架 工作窃取