JUC并发编程学习笔记

目录

一、什么是JUC

2、线程和进程

1.进程

2.线程

3.并发

4.并行

并发编程的本质

3、多线程回顾

1.线程的几种状态

2.sleep和wait的区别

4、Lock锁

1、传统 Synchronized锁

2.公平锁和非公平锁(锁的底层)

3.Synchronized 和 Lock 区别

4.生产者和消费者问题

生产者和消费者问题:synchronized版

问题存在:ABCD4个线程会有虚假唤醒

生产者和消费者问题:JUC版

Condition 精准的通知和唤醒线程

5、8锁现象(就是关于锁的八个问题)

6、集合类不安全

ArryList集合

HashSet集合

HashMap集合

7、Callable接口

8、常用的辅助类(必会)

1.CountDownLatch

2.CyclicBarrier

3.Semaphore

9、读写锁ReadWriteLock

10、阻塞队列  BlockingQueue

四组API

SynchronousQueue同步队列

11、线程池 

池化技术

线程池的好处

线程池:3大方法

线程池:7大参数

手动创建一个线程池 

线程池:4种拒绝策略

IO密集型,CPU密集型:(调优)

12、四大函数式接口(必需掌握)

函数式接口:只有一个方法的接口

Function 函数式接口

Predicate 断定型接口

Consumer 消费型接口

Supplier 供给型接口

13、Stream 流式计算

14、ForkJoin

ForkJoin 

15、异步回调

16、JMM

17、Volatile

1、保证可见性

2、不保证原子性

3、指令重排

18、单例模式

单例模式的几种实现方式

1、懒汉式,线程不安全

2、懒汉式,线程安全

3、饿汉式

4、双检锁/双重校验锁(DCL,即 double-checked locking)

5、登记式/静态内部类

6、枚举

19、深入理解CAS

Unsafe 类 

CAS : ABA 问题(狸猫换太子)

20、原子引用 

21、各种锁的理解

1、公平锁、非公平锁

2、可重入锁

Synchronized 版

Lock 版

3、自旋锁

4、死锁


1、什么是JUC

JUC指的是:Java里的三个包
java.util.concurrent
java.util.concurrent.atomic:原子性
java.util.concurrent.locks:lock锁

2、线程和进程

1.进程

程序执行的一次过程,一个进程包含一个或多个线程。进程是资源分配的单位。

2.线程

可以指程序执行过程中,负责实现某个功能的单位。线程是CPU调度和执行的单位。

3.并发

同一时刻,多个线程交替执行。(一个CPU交替执行线程)

4.并行

同一时刻,多个线程同时执行。(多个CPU同时执行多个线程)

# 获取cpu的核数(cpu密集型;io密集型)
System.out.println(Runtime.getRuntime().availableProcessors());

并发编程的本质

并发编程的本质是充分利用cpu资源。

Java 真的可以开启线程吗? 答案是:开不了的

Java创建Thread类调用start方法,底层是把线程放到一个组里面,然后调用一个本地方法start0;方法底层是C++;Java无法操作硬件。

3、多线程回顾

1.线程的几种状态

public enum State {
    /**
     * Thread state for a thread which has not yet started.
     * 线程新生状态
     */
    NEW,
    /**
     * Thread state for a runnable thread.  A thread in the runnable
     * state is executing in the Java virtual machine but it may
     * be waiting for other resources from the operating system
     * such as processor.
     * 线程运行中
     */
    RUNNABLE,
    /**
     * Thread state for a thread blocked waiting for a monitor lock.
     * A thread in the blocked state is waiting for a monitor lock
     * to enter a synchronized block/method or
     * reenter a synchronized block/method after calling
     * {@link Object#wait() Object.wait}.
     * 线程阻塞状态
     */
    BLOCKED,
    /**
     * Thread state for a waiting thread.
     * A thread is in the waiting state due to calling one of the
     * following methods:
     * <ul>
     *   <li>{@link Object#wait() Object.wait} with no timeout</li>
     *   <li>{@link #join() Thread.join} with no timeout</li>
     *   <li>{@link LockSupport#park() LockSupport.park}</li>
     * </ul>
     *
     * <p>A thread in the waiting state is waiting for another thread to
     * perform a particular action.
     *
     * For example, a thread that has called <tt>Object.wait()</tt>
     * on an object is waiting for another thread to call
     * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
     * that object. A thread that has called <tt>Thread.join()</tt>
     * is waiting for a specified thread to terminate.
     * 线程等待状态,死等
     */
    WAITING,
    /**
     * Thread state for a waiting thread with a specified waiting time.
     * A thread is in the timed waiting state due to calling one of
     * the following methods with a specified positive waiting time:
     * <ul>
     *   <li>{@link #sleep Thread.sleep}</li>
     *   <li>{@link Object#wait(long) Object.wait} with timeout</li>
     *   <li>{@link #join(long) Thread.join} with timeout</li>
     *   <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
     *   <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
     * </ul>
     * 线程超时等待状态,超过一定时间就不再等
     */
    TIMED_WAITING,
    /**
     * Thread state for a terminated thread.
     * The thread has completed execution.
     * 线程终止状态,代表线程执行完毕
     */
    TERMINATED;
}

2.sleep和wait的区别

  1. sleep是Thread类的本地方法;wait是Object类的方法。
  2. sleep不释放锁;wait释放锁。
  3. sleep不需要和synchronized关键字一起使用;wait必须和synchronized代码块一起使用。
  4. sleep不需要被唤醒(时间到了自动退出阻塞);wait需要被唤醒。
  5. sleep一般用于当前线程休眠,或者轮循暂停操作;wait则多用于多线程之间的通信。

4、Lock锁

1、传统 Synchronized锁

public class SaleTicketTDemo01 {
    /*
     * 真正的多线程开发,公司中的开发,降低耦合性
     * 线程就是一个单独的资源类,没有任何附属的操作!
     * 1、 属性、方法
     */
    public static void main(String[] args) {
        //并发:多个线程同时操作一个资源类,把资源类丢入线程
        Ticket ticket = new Ticket();
        // @FunctionalInterface 函数式接口,jdk1.8 lambada表达式
        new Thread(() -> {
            for (int i = 1; i < 50; i++) {
                ticket.sale();
            }
        }, "A").start();
        new Thread(() -> {
            for (int i = 1; i < 50; i++) {
                ticket.sale();
            }
        }, "B").start();
        new Thread(() -> {
            for (int i = 1; i < 50; i++) {
                ticket.sale();
            }
        }, "C").start();
    }
}
//资源类 OOP
class Ticket {
    //属性、方法
    private int number = 50;
    // 卖票的方式
    // synchronized 本质: 队列,锁
    public synchronized void sale() {
        if (number > 0) {
            System.out.println(Thread.currentThread().getName() + "卖出了" +
                    (50-(--number)) + "张票,剩余:" + number + "张票");
        }
    }
}

2.公平锁和非公平锁(锁的底层)

  • 公平锁:十分公平,线程执行顺序按照先来后到顺序
  • 非公平锁:十分不公平:可以插队 (默认锁)

将上面的卖票例子用lock锁 替换synchronized:

public class SaleTicketTDemo02 {
    public static void main(String[] args) {
        //并发:多个线程同时操作一个资源类,把资源类丢入线程
        Ticket2 ticket = new Ticket2();
        // @FunctionalInterface 函数式接口,jdk1.8 lambada表达式
        new Thread(() -> {
            for (int i = 1; i < 50; i++) {
                ticket.sale();
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值