JUC并发编程
什么是JUC
- java.util下的三个包
进程和线程
一个程序是一个进程
一个进程可以有多个线程,至少有一个线程。
java默认有两个线程。 main GC
java不可以开启线程
public synchronized void start() {
/**
* This method is not invoked for the main method thread or "system"
* group threads created/set up by the VM. Any new functionality added
* to this method in the future may have to also be added to the VM.
*
* A zero status value corresponds to state "NEW".
*/
if (threadStatus != 0)
throw new IllegalThreadStateException();
/* Notify the group that this thread is about to be started
* so that it can be added to the group's list of threads
* and the group's unstarted count can be decremented. */
group.add(this);
boolean started = false;
try {
start0();
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
/* do nothing. If start0 threw a Throwable then
it will be passed up the call stack */
}
}
}
//本地方法,底层c++,java无法直接操作硬件
private native void start0();
并发:交替进行,分享时间片
并行:CPU多核;线程池
Runtime.getRuntime().availableProcessors();
并发编程:充分利用CPU资源
-
线程的几个状态:6个
public enum State { //新生 NEW, //运行 RUNNABLE, //阻塞 BLOCKED, //等待 WAITING, //超时等待 TIMED_WAITING, //终止 TERMINATED; }
-
wait/sleep区别
- 来自不同类: wait =>Object,sleep=>Thread
- 锁的释放:wait 释放锁,sleep抱着锁睡,不释放
- 使用范围不同:wait:同步代码块中,sleep:任何地方都可以
Lock锁(重点)
传统synchronized锁
public class Juc01 {
public static void main(String[] args) {
Ticket ticket = new Ticket();
new Thread(() -> {
for (int i = 0; i < 40; i++) {
ticket.sale();
}
},"A").start();
new Thread(() -> {
for (int i = 0; i < 40; i++) {
ticket.sale();
}
},"B").start();
new Thread(() -> {
for (int i = 0; i < 40; i++) {
ticket.sale();
}
},"C").start();
}
}
//OOP
class Ticket{
private int number = 30;
public synchronized void sale(){
if (number>0){
System.out.println(Thread.currentThread().getName()+"卖出第"+number--+"票,剩余"+number+"张票");
}
}
}
Lock锁
公平锁:顺序
非公平锁:可以插队(默认)
public class Juc02 {
public static void main(String[] args) {
Ticket02 ticket = new Ticket02();
new Thread(() -> { for (int i = 0; i < 40; i++) ticket.sale();} ,"A").start();
new Thread(() -> { for (int i = 0; i < 40; i++) ticket.sale();} ,"B").start();
new Thread(() -> { for (int i = 0; i < 40; i++) ticket.sale();} ,"C").start();
}
}
/*
1.创建锁 Lock lock = new ReentrantLock();
2.加锁 lock.lock();//上锁
3.解锁 finally --> lock.unlock();//解锁
*/
class Ticket02{
private int number = 30;
Lock lock = new ReentrantLock();
public void sale(){
lock.lock();//上锁
try {
//代码块
if (number>0){
System.out.println(Thread.currentThread().getName()+"卖出第"+number--+"票,剩余"+number+"张票");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();//解锁
}
}
}
Synchronized 和 Lock 区别
- Synchronized 内置 java 关键字,Lock是一个接口
- Synchronized 无法判断锁的状态,Lock可以判断是否获取锁
- Synchronized 会自动释放锁,Lock必须手动释放,不释放会死锁
- Synchronized 线程1(获得锁、阻塞),线程2(等待、继续等待);Lock锁不一定会等待下去,
lock.tryLock();
- Synchronized 可重入锁,不可以中断,非公平;Lock,可重入锁,可以判断锁状态,非公平(可设置)
- Synchronized 适合锁少量同步代码块,Lock 可以锁大量代码块
生产者和消费者
面试问题:单例模式、排序算法、生产者消费者、死锁问题
生产者和消费者 Synchronized 版
public class Juc03 {
public static void main(String[] args) {
Data data = new Data();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"A").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.dccrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"B").start();
}
}
class Data{
private int number = 0;
public synchronized void increment() throws InterruptedException {
if (number != 0){
//等待
this.wait();
}
number++;
System.out.println(Thread.currentThread().getName()+"=>"+number);
this.notifyAll();
}
public synchronized void dccrement() throws InterruptedException {
if (number == 0){
//等待
this.wait();
}
number--;
System.out.println(Thread.currentThread().getName()+"=>"+number);
this.notifyAll();
}
}
问题:两个线程对 number++ 会出现虚假唤醒 if 改为 while
public class Juc03 {
public static void main(String[] args) {
Data data = new Data();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"A").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.dccrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"B").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.dccrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"C").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.dccrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"D").start();
}
}
class Data{
private int number = 0;
public synchronized void increment() throws InterruptedException {
while (number != 0){
//等待
this.wait();
}
number++;
System.out.println(Thread.currentThread().getName()+"=>"+number);
this.notifyAll();
}
public synchronized void dccrement() throws InterruptedException {
while (number == 0){
//等待
this.wait();
}
number--;
System.out.println(Thread.currentThread().getName()+"=>"+number);
this.notifyAll();
}
}
生产者和消费者 JUC 版
Synchronized -> wait -> notifyAll
Lock -> await -> signal
public class Juc04 {
public static void main(String[] args) {
Data04 data = new Data04();
new Thread(()->{
for (int i = 0; i < 10; i++) {
data.increment();
}
},"A").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
data.dccrement();
}
},"B").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
data.increment();
}
},"C").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
data.dccrement();
}
},"D").start();
}
}
class Data04{
private int number = 0;
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
public void increment() {
lock.lock();
try {
while (number != 0){
//等待
condition.await();
}
number++;
System.out.println(Thread.currentThread().getName()+"=>"+number);
condition.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void dccrement() {
lock.lock();
try {
while (number == 0){
//等待
condition.await();
}
number--;
System.out.println(Thread.currentThread().getName()+"=>"+number);
condition.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
Condition实现精准通知
public class Juc05 {
public static void main(String[] args) {
Data05 data = new Data05();
new Thread(()->{
for (int i = 0; i < 10; i++) {
data.printA();
}
},"A").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
data.printB();
}
},"B").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
data.printC();
}
},"C").start();
}
}
class Data05{
private Lock lock = new ReentrantLock();
private Condition condition1 = lock.newCondition();
private Condition condition2 = lock.newCondition();
private Condition condition3 = lock.newCondition();
private int number = 1;
public void printA(){
lock.lock();
// 业务, 判断 -> 执行 -> 通知
try {
while (number != 1){
condition1.await();
}
System.out.println(Thread.currentThread().getName()+"=>AAAA");
number = 2;
condition2.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void printB(){
lock.lock();
// 业务, 判断 -> 执行 -> 通知
try {
while (number != 2){
condition2.await();
}
System.out.println(Thread.currentThread().getName()+"=>AAAA");
number = 3;
condition3.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void printC(){
lock.lock();
// 业务, 判断 -> 执行 -> 通知
try {
while (number != 3){
condition3.await();
}
System.out.println(Thread.currentThread().getName()+"=>AAAA");
number = 1;
condition1.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
八锁现象
/**
* 1.标准情况下,两个线程先执行打电话还是发短信? 1/发短信 2/打电话
* 2.延迟4s,两个线程先执行打电话还是发短信? 1/发短信 2/打电话
* //synchronized 锁的对象是方法的调用者,谁先拿到谁执行
* 3.添加hello普通方法,先执行哪一个? 1/hello 2/发短信 3/打电话
* //普通方法不受锁影响
* 4.两个对象,两把锁,按时间顺序先来
* 5.增加两个静态同步方法(static synchronized),只有一个对象,锁的对象变成了Class
* 6.增加两个静态同步方法(static synchronized),有两个对象,锁的对象还是Class
* 7.一个静态同步方法(static synchronized),锁对象Class;
* 一个普通同步方法(synchronized),锁的是调用者,只有一个对象,先执行后者
* 8.一个静态同步方法(static synchronized),锁对象Class;
* 一个普通同步方法(synchronized),锁的是调用者,有两个对象,先执行后者
*/
public class Juc06 {
public static void main(String[] args){
Phone phone = new Phone();
new Thread(()-> phone.sendMsg()).start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()-> phone.call()).start();
new Thread(()-> phone.hello()).start();
}
}
class Phone{
public synchronized void sendMsg() {
// try {
// TimeUnit.SECONDS.sleep(4);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
System.out.println("发短信");
}
public synchronized void call(){
System.out.println("打电话");
}
public void hello(){
System.out.println("hello");
}
}
小结
new:this 锁的是具体的一个对象
static:Class 锁的是唯一的class模板
集合类不安全
List不安全
//ConcurrentModificationException 并发修改异常
public class Juc07 {
public static void main(String[] args) {
//并发下ArrayList 不安全 synchronized
/**
* 解决方案
* 1.List<String> list = new Vector<>();
* 2.List<String> list = Collections.synchronizedList(new ArrayList<>());
* 3.List<String> list = new CopyOnWriteArrayList<>();
*/
//CopyOnWrite 写入时复制 计算机程序设计领域的一种优化策略
//在写入时避免覆盖,造成数据问题
//CopyOnWriteArrayList vs Vector CopyOnWriteArrayList没有synchronized 效率高
List<String> list = new CopyOnWriteArrayList<>();
for (int i = 0; i < 10; i++) {
new Thread(()->{
list.add(UUID.randomUUID().toString().substring(0,5));
System.out.println(list);
},String.valueOf(i)).start();
}
}
}
Set不安全
/**
* ConcurrentModificationException
* 解决方案:
* 1.Set<Object> set = Collections.synchronizedSet(new HashSet<>());
* 2.Set<String> set = new CopyOnWriteArraySet<>();
*/
public class Juc08 {
public static void main(String[] args) {
//Set<String> set = new HashSet<>();
//Set<Object> set = Collections.synchronizedSet(new HashSet<>());
Set<String> set = new CopyOnWriteArraySet<>();
for (int i = 0; i < 30; i++) {
new Thread(()->{
set.add(UUID.randomUUID().toString().substring(0,5));
System.out.println(set);
},String.valueOf(i)).start();
}
}
}
hashSet底层是什么?
public HashSet() { map = new HashMap<>(); } //add 本质就是Map key无法重复 public boolean add(E e) { return map.put(e, PRESENT)==null; } private static final Object PRESENT = new Object();
Map不安全
//ConcurrentModificationException
public class Juc09 {
public static void main(String[] args) {
//初始化容量,加载因子
//new HashMap<>(16,0.75);
//Map<String,String> map = new HashMap<>();
//Map<String,String> map = Collections.synchronizedMap(new HashMap<>());
Map<String,String> map = new ConcurrentHashMap<>();
for (int i = 1; i <= 30; i++) {
new Thread(()->{
map.put(Thread.currentThread().getName(), UUID.randomUUID().toString().substring(0,5));
System.out.println(map);
},String.valueOf(i)).start();
}
}
}
Callable(重点)
- 可以有返回值
- 可以抛出异常
- 方法不同,
run()
/call()
public class Juc10 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
//new Thread().start();怎么启动
//new Thread(new FutureTask(new MyThread())).start();适配器模式
MyThread thread = new MyThread();
FutureTask futureTask = new FutureTask(thread);
new Thread(futureTask,"A").start();
new Thread(futureTask,"B").start();//存在缓存
Integer o = (Integer) futureTask.get();//可能产生阻塞,等待返回值,可以用异步通信获取结果
System.out.println(o);
}
}
class MyThread implements Callable<Integer>{
@Override
public Integer call() {
System.out.println("call()");
return 1024;
}
}
常用辅助类
CountDownLatch
//计数器
public class Juc11 {
public static void main(String[] args) throws InterruptedException {
//必须要执行的任务的时候要使用。
CountDownLatch countDownLatch = new CountDownLatch(6);
for (int i = 1; i <= 6; i++) {
new Thread(()->{
System.out.println(Thread.currentThread().getName()+"Go Out");
countDownLatch.countDown();//数量-1
},String.valueOf(i)).start();
}
countDownLatch.await();//等待计数器归零,向下执行
System.out.println("Close The Door");
}
}
原理:
countDownLatch.countDown();
//数量+1
countDownLatch.await();
//等待计数器归零
每次有线程调用countDown()
数量-1,等待计数器变为0,countDownLatch.await()
被唤醒,继续向下执行。
CyclicBarrier
public class Juc12 {
public static void main(String[] args) {
/**
* 集齐七颗龙珠召唤神龙
*/
CyclicBarrier cyclicBarrier = new CyclicBarrier(7,()->{
System.out.println("召唤神龙");
});
for (int i = 1; i <= 7; i++) {
final int temp = i;//lambda取不到i值
new Thread(()->{
System.out.println(Thread.currentThread().getName()+"收集"+temp+"个龙珠");
try {
cyclicBarrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}).start();
}
}
}
Semaphore
一个计数信号量。 在概念上,信号量维持一组许可证。 如果有必要,每个acquire()
都会阻塞,直到许可证可用,然后才能使用它。 每个release()
添加许可证,潜在地释放阻塞获取方。 但是,没有使用实际的许可证对象; Semaphore只保留可用数量的计数,并相应地执行。
public class Juc13 {
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(2);
System.out.println(Thread.currentThread().getName()+"离开车位");
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
semaphore.release();//释放
}
},String.valueOf(i)).start();
}
}
}
ReadWriteLock
/**
* 独占锁(写锁)一次只有一个线程占有
* 共享锁(读锁)多个线程可以同时占有
* ReadWriteLock
* 读-读(可以共存)
* 读-写(不能共存)
* 写-写(不能共存)
*/
public class Juc14 {
public static void main(String[] args) {
MyCacheLock myCache = new MyCacheLock();
//写入
for (int i = 0; i < 5; i++) {
final int temp = i;
new Thread(()->{
myCache.put(temp+"",temp+"");
},String.valueOf(i)).start();
}
//读取
for (int i = 0; i < 5; i++) {
final int temp = i;
new Thread(()->{
myCache.get(temp+"");
},String.valueOf(i)).start();
}
}
}
class MyCacheLock{
private volatile Map<String,String> map = new HashMap<>();
private ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
//存,写
public void put(String key, String value){
readWriteLock.writeLock().lock();
try {
System.out.println(Thread.currentThread().getName()+"写入"+key);
map.put(key,value);
System.out.println(Thread.currentThread().getName()+"写入ok");
} catch (Exception e) {
e.printStackTrace();
} finally {
readWriteLock.writeLock().unlock();
}
}
//取,读
public void get(String key){
readWriteLock.readLock().lock();
try {
System.out.println(Thread.currentThread().getName()+"读取"+key);
String s = map.get(key);
System.out.println(Thread.currentThread().getName()+"读取ok");
} catch (Exception e) {
e.printStackTrace();
} finally {
readWriteLock.readLock().unlock();
}
}
}
/**
* 自定义缓存
*/
class MyCache{
private volatile Map<String,String> map = new HashMap<>();
//存,写
public void put(String key, String value){
System.out.println(Thread.currentThread().getName()+"写入"+key);
map.put(key,value);
System.out.println(Thread.currentThread().getName()+"写入ok");
}
//取,读
public void get(String key){
System.out.println(Thread.currentThread().getName()+"读取"+key);
String s = map.get(key);
System.out.println(Thread.currentThread().getName()+"读取ok");
}
}
阻塞队列
BlockingQueue:用于多并发处理,线程池!
四组API
- 抛出异常
- 不会抛出异常
- 阻塞 等待
- 超时等待
方法 | 抛出异常 | 不抛出异常,返回值 | 阻塞 等待 | 超时等待 |
---|---|---|---|---|
添加 | add() | offer() | put() | offer(…) |
移除 | remove() | poll() | take() | poll(…) |
判断队列首 | element() | peek() | - | - |
public class Juc15 {
public static void main(String[] args) throws InterruptedException {
test4();
}
/**
* 抛出异常
*/
public static void test1(){
BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
System.out.println(blockingQueue.add("a"));//true
System.out.println(blockingQueue.add("b"));//true
System.out.println(blockingQueue.add("c"));//true
//java.lang.IllegalStateException: Queue full抛出异常
//System.out.println(blockingQueue.add("d"));
System.out.println(blockingQueue.element());
System.out.println(blockingQueue.remove());//a
System.out.println(blockingQueue.remove());//b
System.out.println(blockingQueue.remove());//c
//java.util.NoSuchElementException 抛出异常
//System.out.println(blockingQueue.remove());
}
/**
* 不抛出异常,有返回值
*/
public static void test2(){
BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
System.out.println(blockingQueue.offer("a"));//true
System.out.println(blockingQueue.offer("b"));//true
System.out.println(blockingQueue.offer("c"));//true
System.out.println(blockingQueue.offer("d"));//false
System.out.println(blockingQueue.peek());
System.out.println(blockingQueue.poll());//a
System.out.println(blockingQueue.poll());//b
System.out.println(blockingQueue.poll());//c
System.out.println(blockingQueue.poll());//bull
}
/**
* 一直等待
*/
public static void test3() throws InterruptedException {
BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
blockingQueue.put("a");
blockingQueue.put("b");
blockingQueue.put("c");
//blockingQueue.put("d");
System.out.println(blockingQueue.take());
System.out.println(blockingQueue.take());
System.out.println(blockingQueue.take());
//System.out.println(blockingQueue.take());
}
/**
* 超时等待
*/
public static void test4() throws InterruptedException {
BlockingQueue blockingQueue = new ArrayBlockingQueue(3);
blockingQueue.offer("a");
blockingQueue.offer("b");
blockingQueue.offer("c");
//blockingQueue.offer("d",2, TimeUnit.SECONDS);//超时2秒退出
blockingQueue.poll();
blockingQueue.poll();
blockingQueue.poll();
//blockingQueue.poll(2,TimeUnit.SECONDS);//超时2秒退出
}
}
同步队列
**SynchronousQueue:**没有容量,进去一个,出来一个,一个队列只允许一个线程
/**
* 同步队列
* 和其他BlockingQueue不同,SynchronousQueue不存储元素
* put一个元素,必须take一个出来,不然不能再put其他元素
*/
public class Juc16 {
public static void main(String[] args) {
BlockingQueue<String> blockingQueue = new SynchronousQueue<>();
new Thread(()->{
try {
System.out.println(Thread.currentThread().getName()+" put 1");
blockingQueue.put("1");
System.out.println(Thread.currentThread().getName()+" put 2");
blockingQueue.put("2");
System.out.println(Thread.currentThread().getName()+" put 3");
blockingQueue.put("3");
} catch (InterruptedException e) {
e.printStackTrace();
}
},"T1").start();
new Thread(()->{
try {
TimeUnit.SECONDS.sleep(2);
System.out.println(Thread.currentThread().getName()+"=>"+blockingQueue.take());
TimeUnit.SECONDS.sleep(2);
System.out.println(Thread.currentThread().getName()+"=>"+blockingQueue.take());
TimeUnit.SECONDS.sleep(2);
System.out.println(Thread.currentThread().getName()+"=>"+blockingQueue.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
},"T2").start();
}
}
线程池(重点)
线程池:3大方法、7大参数、4种拒绝策略
池化技术
程序的运行会占用资源,优化资源的使用。
线程池、连接池、内存池、对象池…
池化技术:事先准备好一些资源,用的时候直接取,减少创建、销毁消耗资源。
线程池的好处:
- 降低资源消耗
- 提高响应速度
- 方便管理
线程可以复用、可以控制最大并发数、管理线程
线程池三大方法
//Executors 工具类,3大方法
public class Juc17 {
public static void main(String[] args) {
ExecutorService threadPool = Executors.newSingleThreadExecutor();//Single 单个线程池
//ExecutorService threadPool = Executors.newFixedThreadPool(5);//固定大小的线程池
//ExecutorService threadPool = Executors.newCachedThreadPool();//可伸缩线程池
try {
for (int i = 0; i < 12; i++) {
//使用线程池创建线程
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName()+"ok");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
threadPool.shutdown();
}
}
}
7大参数
int corePoolSize,//核心线程池大小 int maximumPoolSize,//最大核心线程大小 long keepAliveTime,//超时了没有人调用会释放 TimeUnit unit,//超时单位 BlockingQueue<Runnable> workQueue,//阻塞队列 ThreadFactory threadFactory,//线程工程,创建线程 RejectedExecutionHandler handler//拒绝策略
源码分析
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
//本质:ThreadPoolExecutor
public ThreadPoolExecutor(int corePoolSize,//核心线程池大小
int maximumPoolSize,//最大核心线程大小
long keepAliveTime,//超时了没有人调用会释放
TimeUnit unit,//超时单位
BlockingQueue<Runnable> workQueue,//阻塞队列
ThreadFactory threadFactory,//线程工程,创建线程
RejectedExecutionHandler handler//拒绝策略) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.acc = System.getSecurityManager() == null ?
null :
AccessController.getContext();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
四种拒绝策略
1.
ThreadPoolExecutor.AbortPolicy()
//超出最大承载数会抛出异常,RejectedExecutionException
2.ThreadPoolExecutor.CallerRunsPolicy()
//超出最大承载数从哪来回哪去
3.ThreadPoolExecutor.DiscardPolicy()
//超出最大承载数不会抛出异常,丢掉任务
4.ThreadPoolExecutor.DiscardOldestPolicy()
//超出最大承载数不会抛出异常,丢掉最早的竞争
//自定义线程池创建
public class Juc18 {
public static void main(String[] args) {
//自定义线程池!
/**
int corePoolSize,//核心线程池大小
int maximumPoolSize,//最大核心线程大小
long keepAliveTime,//超时了没有人调用会释放
TimeUnit unit,//超时单位
BlockingQueue<Runnable> workQueue,//阻塞队列
ThreadFactory threadFactory,//线程工程,创建线程
RejectedExecutionHandler handler//拒绝策略
*/
ExecutorService threadPool = new ThreadPoolExecutor(
2,
5,
3,
TimeUnit.SECONDS,
new LinkedBlockingQueue<>(3),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.DiscardOldestPolicy()//不会抛出异常,尝试和最早的竞争
);
try {
//最大承载数:Queue + max
for (int i = 0; i < 9; i++) {
//使用线程池创建线程
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName()+" ok");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
threadPool.shutdown();
}
}
}
最大线程到底该如何定义:
1.CPU密集型 服务器是几核就设置为几,可以保持CPU效率最高,查看电脑核数Runtime.getRuntime().availableProcessors();
2.IO密集型 判断程序中IO的线程,设置双倍
四大函数式接口(必须掌握)
新时代程序员:lambda表达式、(函数式)链式编程、函数式接口、Stream流式计算
函数式接口
@FunctionalInterface
public interface Runnable {
public abstract void run();
}
//FunctionalInterface 很多函数式接口
//简化编程模型,在新版本的框架底层中大量应用
//forEach(消费型类函数式接口)
代码测试:
Function 函数型
/**
* Function 函数型接口,有一个参数一个输出
* 只要是函数型接口都可以使用lambda表达式
*/
public class Juc19 {
public static void main(String[] args) {
// Function function = new Function<String, String>() {
// @Override
// public String apply(String str) {
// return str;
// }
// };
Function<String, String> function = (str)->{return str;};
System.out.println(function.apply("abc"));
}
}
Predicate 断定型
/**
* 断定型接口,有一个输入参数,返回值是布尔值类型
*/
public class Juc20 {
public static void main(String[] args) {
//判断字符串是否为空
// Predicate<String> predicate = new Predicate<String>() {
// @Override
// public boolean test(String str) {
// return str.isEmpty();
// }
// };
Predicate<String> predicate = (str)->{return str.isEmpty();};
System.out.println(predicate.test(""));
}
}
Consumer 消费型
/**
* Consumer 消费型接口:只有输入 没有返回值
*/
public class Juc21 {
public static void main(String[] args) {
// Consumer<String> consumer = new Consumer<String>() {
// @Override
// public void accept(String str) {
// System.out.println(str);
// }
// };
Consumer<String> consumer = (str)->{System.out.println(str);};
consumer.accept("abc");
}
}
Supplier 供给型
/**
* Supplier 供给型接口: 没有参数只有返回值
*/
public class Juc22 {
public static void main(String[] args) {
// Supplier<String> supplier = new Supplier<String>() {
// @Override
// public String get() {
// return "1024";
// }
// };
Supplier<String> supplier =()->{return "1024";};
System.out.println(supplier.get());
}
}
Stream流式计算
什么是Stream流式计算
大数据 :存储 + 计算
存储:集合 、 MySQL
计算都应该交给流来操作!
/**
* 题目要求:一分钟完成此题,只能用一行代码
* 现在有5个用户
* 1.ID 必须是偶数
* 2.年龄必须大于23
* 3.用户名字转为大写
* 4.用户名字母倒着排序
* 5.只输出一个用户
*/
public class StreamDemo {
public static void main(String[] args) {
User u1 = new User(1,"a",21);
User u2 = new User(2,"b",22);
User u3 = new User(3,"c",23);
User u4 = new User(4,"d",24);
User u5 = new User(6,"e",25);
//集合就是存储
//List<User> list = Arrays.asList(u1,u2,u3,u4,u5);
//计算交给流
// lambda表达式、链式编程、函数式接口、Stream流式计算
Arrays.asList(u1,u2,u3,u4,u5)
.stream()
.filter(user -> {return user.getId()%2==0;})
.filter(user -> {return user.getAge()>23;})
.map(user -> {return user.getName().toUpperCase();})
.sorted((user1,user2)->{return user2.compareTo(user1);})
.limit(1)
.forEach(System.out::println);
}
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class User{
private int id;
private String name;
private int age;
}
ForkJoin
什么是ForkJoin?
ForkJoin在JDK1.7,并行执行任务!提高效率,大数据量!
大数据:Map Reduce(把大任务拆分小任务)
ForkJoin特点 :工作窃取 双端队列
/**
* 如何使用forkJoin
* 1.forkJoinPool 通过它来执行
* 2.计算任务 forkJoinPool.execute(ForkJoinTask task)
* 3.计算类要继承 ForkJoinTask
*/
public class ForkJoinDemo extends RecursiveTask<Long> {
private Long start;
private Long end;
//临界值
private Long temp = 10000L;
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 middle = start + (end-start)/2;
ForkJoinDemo task1 = new ForkJoinDemo(start, middle);
task1.fork();
ForkJoinDemo task2 = new ForkJoinDemo(middle + 1, end);
task2.fork();
return task1.join() + task2.join();
}
}
}
class Main{
public static void main(String[] args) throws Exception {
//test1();//16319
//test2();//10738
test3();//225
}
public static void test1(){
Long sum = 0L;
long start = System.currentTimeMillis();
for (Long i = 1L; i < 10_0000_0000; i++) {
sum += i;
}
long end = System.currentTimeMillis();
System.out.println("sum = "+sum+"时间:"+(end-start));
}
public static void test2() throws Exception {
long start = System.currentTimeMillis();
ForkJoinPool forkJoinPool = new ForkJoinPool();
ForkJoinDemo task = new ForkJoinDemo(0L, 10_0000_0000L);
ForkJoinTask<Long> submit = forkJoinPool.submit(task);
Long sum = submit.get();
long end = System.currentTimeMillis();
System.out.println("sum = "+sum+"时间:"+(end-start));
}
public static void test3(){
long start = System.currentTimeMillis();
long sum = LongStream.rangeClosed(0L, 10_0000_0000L).parallel().reduce(0, Long::sum);
long end = System.currentTimeMillis();
System.out.println("sum = "+sum+"时间:"+(end-start));
}
}
异步回调
public class FutureDemo {
public static void main(String[] args) throws Exception {
//没有返回值的异步回调 runAsync
// CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
// try {
// TimeUnit.SECONDS.sleep(3);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// System.out.println(Thread.currentThread().getName() + "runAsync=>Void");
// });
// System.out.println("1111");
// completableFuture.get();//获取阻塞执行结果
// 有返回值的 supplyAsync 异步回调
CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + "supplyAsync=>Integer");
//int i = 10/0;
return 1024;
});
System.out.println(completableFuture.whenComplete((t, u) -> {
System.out.println("t=>" + t);
System.out.println("u=>" + u);
}).exceptionally((e) -> {
System.out.println(e.getMessage());
return 233;
}).get());
}
}
JMM
请你谈谈你对Volatile的理解
Volatile 是虚拟机提供轻量级的同步机制
- 保证可见性
- 不保证原子性
- 禁止指令重排
什么是JMM
JMM :java内存模型,不存在,是概念,一种约定。
关于 JMM 的一些同步的约定:
- 线程解锁时,必须把共享变量立刻刷新回主存。
- 线程加锁前,必须读取主存中的最新值到工作内存中。
- 加锁和解锁必须是同一把锁。
线程:工作内存、主内存
8种操作:
问题:线程B修改值后,线程A不能及时可见
关于主内存与工作内存之间的具体交互协议,即一个变量如何从主内存拷贝到工作内存,如何从工作内存同步回主内存子类的细节实现,java内存模型定义了八种操作:(这八个操作都具有原子性)
- lock(锁定):作用于主内存的变量,把一个变量标识为一条线程独占的状态。
- unclock(解锁):作用于主内存的变量,把一个处于锁定的状态释放出来。
- read(读取):作用于主内存的变量,把一个变量的值从主内存传输到线程的工作内存中
- load(载入):作用于工作内存的变量,把read操作从主内存 得到的变量值放入工作内存的变量副本中。
- use(使用):作用于工作内存的变量,把工作内存中一个变量的值传递给执行引擎,每当虚拟机遇到一个需要使用到变量的值的字节码指令时将会执行这个操作。
- assign(赋值):作用于工作内存的变量,把一个从执行引擎接收到的值 赋值给工作内存的变量,每当虚拟机遇到一个给变量赋值的字节码指令时执行这个操作。
- store(存储):作用于工作内存的变量,把工作内存中的一个变量的值传递到主内存,以便write操作使用。
- write(写入):作用于主内存的变量,把store操作从工作内存中得到的变量的值放入主内存的变量中。
Java内存模型对上述8种操作有如下的约束:
- 把一个变量从主内存复制到工作内存中必须顺序执行read读入操作和load载入操作。把一个变量从工作内存同步回主内存中必须顺序执行store存储操作和write写入操作。 read和load操作之间、store和write操作之间可以插入其他指令,但是read和load操作、store和write操作必须要按顺序执行,即不允许read和load、store和write操作之一单独出现。
- 不允许一个线程丢弃它的最近的assign赋值操作,即工作内存变量值改变之后必须同步回主内存。只有发生过assign赋值操作的变量才需要从工作内存同步回主内存。
- 一个新变量只能在主内存中产生,不允许在工作内存中直接使用一个未被初始化(load或assign)的变量,即一个变量在进行use和store操作之前,必须先执行过assgin和load操作。
- 一个变量在同一时刻只允许一条线程对其进行lock锁定操作,但是lock锁定可以被一条线程重复执行多次,多次执行lock之后,只有执行相同次数的unlock操作变量才会被解锁。
- 如果对一个变量执行lock锁定操作,将会清空工作内存中该变量的值,在执行引擎使用这个变量前,需要重新执行load或assign操作初始化变量的值。
- 如果一个变量事先没有被lock锁定,则不允许对这个变量进行unlock解锁操作,也不允许对一个被别的线程锁定的变量进行unlock解锁。
- 一个变量进行unlock解锁操作之前,必须先把此变量同步回主内存中(执行store和write操作)。
问题:程序不知道主内存值内修改了 ==>Volatile
Volatile
保证可见性
public class JMMdemo01 {
//不加 volatile 程序死循环
//加 volatile 可以保证可见性
public volatile static int num = 0;
public static void main(String[] args) {
new Thread(()->{
while ( num==0){
}
}).start();
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
num = 1;
System.out.println(num);
}
}
不保证原子性
public class JMMdemo02 {
// volatile 不保证原子性
private volatile static int num = 0;
public static void add(){
num++;
}
public static void main(String[] args) {
for (int i = 0; i < 20; i++) {
new Thread(()->{
for (int i1 = 0; i1 < 1000; i1++) {
add();
}
}).start();
}
while (Thread.activeCount()>2){
Thread.yield();
}
System.out.println(num);//18178
}
}
如果不加Lock 和 synchronize 怎么保证原子性
使用原子类解决
public class JMMdemo02 {
// volatile 不保证原子性
// Integer 原子类
private volatile static AtomicInteger num = new AtomicInteger();
public static void add(){
//num++;
num.getAndIncrement(); //AtomicInteger +1 方法 CAS
}
public static void main(String[] args) {
for (int i = 0; i < 20; i++) {
new Thread(()->{
for (int i1 = 0; i1 < 1000; i1++) {
add();
}
}).start();
}
while (Thread.activeCount()>2){
Thread.yield();
}
System.out.println(num);
}
}
原子类都和操作系统挂钩,在内存中修改值,Unsafe类是一个很特殊的存在!
禁止指令重排
什么是指令重排? 你写的程序并不是按照你写的顺序执行。
源代码 --> 编译器优化重排 --> 指令并行可能重排 --> 内存系统也会重排 --> 执行
int x = 1;//1
int y = 2;//2
x = x + 5;//3
y = x + x;//4
//执行顺序可能是 1234 2134 1324 , 不可能是4321
处理器在执行指令重排的时候,要考虑数据之间的依赖性。
但是两个线程出现指令重排后影响最后的值。
**volatile 可以避免指令重排:**内存屏障,CPU指令
- 保持特定的操作顺序
- 可以保证某些变量的内存可见性
单例模式
饿汉式单例模式
//饿汉单例模式 浪费资源,加载所有资源
public class SingleHungry {
private SingleHungry(){
}
private final static SingleHungry SINGLE_HUNGRY = new SingleHungry();
public static SingleHungry getInstance(){
return SINGLE_HUNGRY;
}
}
懒汉式单例模式
public class SingleLazy {
private SingleLazy(){
System.out.println(Thread.currentThread().getName()+ "ok");
}
//双重检测锁+ volatile 避免指令重排
private volatile static SingleLazy singleLazy;
//双重检测锁模式 DCL懒汉
public static SingleLazy getInstance(){//单线程OK
if (singleLazy == null){
synchronized (SingleLazy.class){
if(singleLazy == null){
singleLazy = new SingleLazy();//不是原子性操作
/**
* 1.分配内存空间
* 2.执行构造方法,初始化对象
* 3.把对象指向空间
* 123
* 132 A
* B //没有构造完成 ,返回空
*/
}
}
}
return singleLazy;
}
//多线程并发
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
new Thread(()->{
SingleLazy.getInstance();
}).start();
}
}
}
静态内部类
public class SingleStaticClass {
private SingleStaticClass(){
}
public static SingleStaticClass getInstace(){
return InnerClass.SINGLE_STATIC_CLASS;
}
public static class InnerClass {
private static final SingleStaticClass SINGLE_STATIC_CLASS = new SingleStaticClass();
}
}
反射破坏单例模式
枚举单例
public enum EnumSingle {
INSTANCE;
public EnumSingle getInstance(){
return INSTANCE;
}
}
class Test{
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
EnumSingle instance1 = EnumSingle.INSTANCE;
//Constructor<EnumSingle> declaredConstructor = EnumSingle.class.getDeclaredConstructor(null);
//juc.EnumSingle.<init>()
Constructor<EnumSingle> declaredConstructor = EnumSingle.class.getDeclaredConstructor(String.class,int.class);
//Cannot reflectively create enum objects 有两个参数的构造方法 枚举不会被反射破坏
declaredConstructor.setAccessible(true);
EnumSingle instance2 = declaredConstructor.newInstance();
System.out.println(instance1);
System.out.println(instance2);
}
}
深入理解CAS
什么是CAS
compareAndSwap: 比较并交换
public class CASDemo {
public static void main(String[] args) {
//CAS compareAndSwap: 比较并交换
AtomicInteger atomicInteger = new AtomicInteger(2020);
//expect 期望 update 更新值
//public final boolean compareAndSet(int expect, int update)
atomicInteger.compareAndSet(2020,2025);
System.out.println(atomicInteger.get());
}
}
Unsafe 类
CAS:比较当前工作内存中的值和主内存中的值,如果这个值是期望的,那么则执行操作,如果不是就一直循环。
缺点:
- 循环会耗时
- 一次性只能保证一个共享变量的原子性
- ABA问题
CAS:ABA问题(狸猫换太子)
public class CASDemo {
public static void main(String[] args) {
//CAS compareAndSwap: 比较并交换
AtomicInteger atomicInteger = new AtomicInteger(1);
//==============线程2====================
System.out.println(atomicInteger.compareAndSet(1, 3));
System.out.println(atomicInteger.get());
System.out.println(atomicInteger.compareAndSet(3, 1));
System.out.println(atomicInteger.get());
//==============线程1====================
System.out.println(atomicInteger.compareAndSet(1, 2));
System.out.println(atomicInteger.get());
}
}
原子引用
解决ABA问题,引入原子引用
带版本号的操作
注意:
Integer使用了对象缓存机制,默认范围是-128 ~ 127 , 推荐使用静态工厂方法valueOf获取对象实例,而不是new,因为valueOf使用缓存,而new -定会创建新的对象分配新的内存空间;
public class CASDemo {
public static void main(String[] args) {
//如果泛型是一个包装类,注意对象引用问题
//正常业务引用的是一个对象
AtomicStampedReference<Integer> stampedReference = new AtomicStampedReference<>(1,1);
new Thread(()->{
int stamp = stampedReference.getStamp();
System.out.println("线程2版本号:"+stamp);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(stampedReference.compareAndSet(1, 3
, stampedReference.getStamp(), stampedReference.getStamp() + 1));
System.out.println("线程2版本号:"+stampedReference.getStamp());
System.out.println(stampedReference.compareAndSet(3, 1
, stampedReference.getStamp(), stampedReference.getStamp() + 1));
System.out.println("线程2版本号:"+stampedReference.getStamp());
},"线程2").start();
//乐观锁原理
new Thread(()->{
int stamp = stampedReference.getStamp();
System.out.println("线程1版本号:"+stamp);
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(stampedReference.compareAndSet(1, 2
, stamp, stamp + 1));
System.out.println("线程1版本号:"+stampedReference.getStamp());
},"线程1").start();
}
}
各种锁的理解
1、公平锁、非公平锁
公平锁:非常公平,不能插队,先来后到。
非公平锁:可以插队。(默认都是非公平锁)
//非公平锁
public ReentrantLock() {
sync = new NonfairSync();
}
//公平锁
public ReentrantLock(boolean fair) {
sync = fair ? new FairSync() : new NonfairSync();
}
2、可重入锁
可重入锁(递归锁)
synchronized
public class Juc23 {
public static void main(String[] args) {
Phone1 phone1 = new Phone1();
new Thread(()->{
phone1.send();
},"A:").start();
new Thread(()->{
phone1.send();
},"B:").start();
}
}
class Phone1{
public synchronized void send(){
System.out.println(Thread.currentThread().getName()+"发短信");
call();
}
public synchronized void call(){
System.out.println(Thread.currentThread().getName()+"打电话");
}
}
Lock
public class Juc24 {
public static void main(String[] args) {
Phone2 phone2 = new Phone2();
new Thread(()->{
phone2.send();
},"A:").start();
new Thread(()->{
phone2.send();
},"B:").start();
}
}
class Phone2{
Lock lock = new ReentrantLock();
public void send(){
lock.lock();
try {
System.out.println(Thread.currentThread().getName()+"发短信");
call();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void call(){
lock.lock();
try {
System.out.println(Thread.currentThread().getName()+"打电话");
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
注意:synchronized 钥匙都一样,Lock锁 是一个锁一把钥匙,一对一对出现,否则会出现死锁现象
3、自旋锁
public class SpinLock {
AtomicReference<Thread> atomicReference = new AtomicReference<>();
public void myLock(){
Thread thread = Thread.currentThread();
System.out.println(Thread.currentThread().getName()+ "==> myLock");
while (!atomicReference.compareAndSet(null,thread)){
}
}
public void myUnLock(){
Thread thread = Thread.currentThread();
System.out.println(Thread.currentThread().getName()+ "==> myUnLock");
atomicReference.compareAndSet(thread,null);
}
}
class SpinLockTest{
public static void main(String[] args) throws InterruptedException {
SpinLock lock = new SpinLock();
new Thread(()->{
lock.myLock();
try {
TimeUnit.SECONDS.sleep(3);
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.myUnLock();
}
},"T1").start();
TimeUnit.SECONDS.sleep(1);
new Thread(()->{
lock.myLock();
try {
TimeUnit.SECONDS.sleep(1);
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.myUnLock();
}
},"T2").start();
}
}
4、死锁排查
public class DeadLock {
public static void main(String[] args) {
String lockA = "lockA";
String lockB = "lockB";
new Thread(new MyLockThread(lockA,lockB),"T1").start();
new Thread(new MyLockThread(lockB,lockA),"T2").start();
}
}
class MyLockThread implements Runnable{
private String lockA;
private String lockB;
public MyLockThread(String lockA, String lockB) {
this.lockA = lockA;
this.lockB = lockB;
}
@Override
public void run() {
synchronized (lockA){
System.out.println(Thread.currentThread().getName() + " Lock:" + lockA + "=>get " + lockB);
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lockB){
System.out.println(Thread.currentThread().getName() + " Lock:" + lockB + "=>get" + lockA);
}
}
}
}
解决
1.使用 jps -l
定位进程号
2.使用 jstack 进程号
查看进程堆栈信息
排查问题:
-
日志
-
堆栈信息