什么是JUC

news/2024/5/20 6:11:31 标签: java, juc

什么是JUC

    JUC指的是:Java里的三个包

  • java.util.concurrent
  • java.util.concurrent.atomic:原子性
  • java.util.concurrent.locks:lock锁

 

回顾线程和进程

进程

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

线程

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

并发

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

并行

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

    查询cpu核数

java">public class Test1 {
    public static void main(String[] args) {
        //查询cpu核数
        //CPU 密集型,IO密集型
        System.out.println(Runtime.getRuntime().availableProcessors());
    }
}

并发编程的本质

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

    问题:Java真的可以开启线程吗

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

Thread部分源码

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();

多线程回顾

线程的几种状态

        新生状态、运行状态、阻塞状态、等待状态(死等)、超时等待状态(过期不候)、停止状态

    Thread.State的源码

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;
    }

sleep和wait的区别

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


http://www.niftyadmin.cn/n/9075.html

相关文章

Kotlin MVVM之Jetpack系列ViewModel、LiveData的简单使用

一、MVVM是什么&#xff1f; MVVM分为Model&#xff0c;View&#xff0c;ViewModel 三个部分 Model:数据层&#xff0c;包含数据实体和对数据实体的操作 View:UI层&#xff0c;对应于Activity&#xff0c;XML&#xff0c;负责数据显示以及用户交互。 ViewModel&#xff1a;…

基于MMDetection训练VOC格式数据集

一 环境说明 基于前述安装MMDetection&#xff0c;数据集为VOC格式&#xff0c;主要版本如下&#xff1a; Python&#xff1a;3.7.8 CUDA&#xff1a;11.3 cuDNN&#xff1a;8.4.0 torch&#xff1a;1.12.0 torchvision&#xff1a;0.13.0 mmcv-full&#xff1a;1.6.0 MMDetec…

BDD - SpecFlow Page Object Model POM

BDD - SpecFlow Page Object Model引言POM 优势POM 简单实现POM 缓存实现POM 层次实现Parent - Child 关系Component 关系引言 前面文章《 BDD - SpecFlow Web UI 测试实践 》就运用到 Page Object Model&#xff0c;简称 POM&#xff0c;POM 是一种模式&#xff0c;结合 Sele…

【附源码】Python计算机毕业设计网上宠物商店系统

项目运行 环境配置&#xff1a; Pychram社区版 python3.7.7 Mysql5.7 HBuilderXlist pipNavicat11Djangonodejs。 项目技术&#xff1a; django python Vue 等等组成&#xff0c;B/S模式 pychram管理等等。 环境需要 1.运行环境&#xff1a;最好是python3.7.7&#xff0c;…

可观测性-可视化-Grafana热图Heatmap

文章目录可观测性-可视化-Grafana热图Heatmap前言选项可观测性-可视化-Grafana热图Heatmap 前言 Heatmap&#xff08;热图&#xff09;的用途&#xff0c;在Grafana官网是这样描述的&#xff1a;使用热图&#xff0c;将允许您查看随时间变化的直方图。 所以要使用热图的前提…

【Python开发】Flask开发实战:个人博客(四)

Flask开发实战&#xff1a;个人博客&#xff08;四&#xff09;本篇博客将是《Flask开发实战&#xff1a;个人博客》的最后一篇。本篇文章将会详细介绍博客后台的编写。 为了支持管理员管理文章、分类、评论和链接&#xff0c;我们需要提供后台管理功能。通常来说&#xff0c;…

labview 串口通信 modbusRtu

在自动化或测试项目中&#xff0c;上位机软件需要和PLC及仪表通信&#xff0c;本文简单描述这个问题。 1.在程序框图中放置4个图标 &#xff08;1&#xff09;创建modbus 主站实例&#xff08;按如下图标识①,在框图中放Create Master Instance.vi) 图1 放置四个图标 &…

网络安全基础知识

web服务器产品 apache-httpd tomcat iis lighttp nginx 不是用来web服务器&#xff0c;而是用来做反向代理&#xff08;tps10w&#xff0c;优化tqs2020w&#xff09; 网络 IP地址和子网掩码 IPv4&#xff1a;32为长度的二进制数字表示&#xff0c;每段用8个字节表示&#x…