一、JUC入门

1.1 JUC是啥?

JUC指的是java.util.concurrent包的简称。是一个处理线程的工具包,JDK1.5出现。在《Java并发编程的艺术》书中,写道本质上提供了两种语义,作为实现concurrent包的实现基石:

  1. CAS 写-读内存语义
  2. volatile 写-读内存语义
    在这里插入图片描述

1.2 进程和线程是啥?

1.2.1 进程

进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础。 在当代面向线程设计的计算机结构中,进程是线程的容器。程序是指令、数据及其组织形式的描述,进程是程序的实体。

1.2.2 线程

线程(Thread) 是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务。

  • 进程:指在系统中正在运行的一个应用程序;程序一旦运行就是进程;进程——资源分配的最小单位。
  • 线程:系统分配处理器时间资源的基本单元,或者说进程之内独立执行的一个单元执行流。线程——程序执行的最小单位。

1.3 线程的状态

操作系统中进程状态转换图
Java中的线程状态转换图

1.3.1 看看Java中的状态枚举类

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;	//终止态
}

1.3.2 wait()与sleep的区别

  1. sleep 是 Thread 的静态方法, wait 是 Object 的方法,任何对象实例都能调用。
  2. sleep 不会释放锁,它也不需要占用锁。 wait 会释放锁,但调用它的前提是当前线程占有锁(即代码要在 synchronized 中)。
  3. 它们都可以被 interrupted() 方法中断。

1.4 并发与并行

  • 并发:在同一时刻,多个线程访问统一资源,多个线程集火一个点。(底层还是交替执行,只是宏观上感觉是同时)
  • 并行:在同一时间段呢,多个线程一起执行

1.5 管程(Monitor)

是一种同步机制,保证同一时刻只有一个进程再管程内活动,管程定义的操作同一时刻只能被进程能调用。但是这样并不能保证进程以设计的顺序执行
JVM中同步基于进入和退出管程对象,每个对象都会有一个管程(monitor)对象,管程(monitor)会随着 java 对象一同创建和销毁
执行线程首先要持有管程对象,然后才能执行方法,当方法完成之后会释放管程,方法在执行时候会持有管程,其他线程无法再获取同一个管程

java
操作系统监视器(Monitor)

1.6 用户线程和守护线程

用户线程:平时用到的普通线程,自定义线程
守护线程:运行在后台,是一种特殊的线程,比如垃圾回收

1.6.1 用户线程代码

/**
 * @author LWJ
 * @date 2023/6/17
 */
public class CustomThreadAndDaemonThread {
	//当主线程结束后,用户线程还在运行,JVM 存活
    public static void main(String[] args) {
        //创建一个用户线程
        new Thread(()->{
            //isDaemon() 方法,判断一个线程是否为守护线程
            System.out.println(Thread.currentThread().getName() + "=====" + Thread.currentThread().isDaemon());
            while (true){}
        },"myThread").start();
        System.out.println(Thread.currentThread().getName() +"====="+"over...");
    }
}

在这里插入图片描述

1.6.2 守护线程代码

/**
 * @author LWJ
 * @date 2023/6/17
 */
public class CustomThreadAndDaemonThread {
	//如果没有用户线程,都是守护线程,JVM 结束 
    public static void main(String[] args) {
        //创建一个用户线程
        Thread myThread = new Thread(() -> {
            //isDaemon() 方法,判断一个线程是否为守护线程
            System.out.println(Thread.currentThread().getName() + "=====" + Thread.currentThread().isDaemon());
            while (true) {
            }
        }, "myThread");
        myThread.setDaemon(true);
        myThread.start();
        System.out.println(Thread.currentThread().getName() +"====="+"over...");
    }
}


如果不存在用户线程了,都是守护线程,JVM 将会停止

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值