什么是JUC

article/2025/10/12 7:04:23

什么是JUC

    JUC指的是:Java里的三个包

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

 

回顾线程和进程

进程

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

线程

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

并发

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

并行

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

    查询cpu核数

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部分源码

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 thenit will be passed up the call stack */}}}//本地方法,底层的C++ java无法直接操作硬件private native void start0();

多线程回顾

线程的几种状态

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

    Thread.State的源码

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://chatgpt.dhexx.cn/article/7SFZ92kT.shtml

相关文章

Java - JUC详解

目录 一、了解和JUC相关的概念 二、Java线程 三、线程共享模型 一、了解和JUC相关的概念 1.1 什么是JUC&#xff1f; JUC是java.util.concurrent包的简称&#xff0c;在Java5.0添加&#xff0c;目的就是为了更好的支持高并发任务。让开发者进行多线程编程时减少竞争条件和…

JUC线程池

一、JUC介绍 java.util.concurrent包&#xff08;简称&#xff1a;JUC&#xff09;。JUC主要是让开发者在多线程编程中更加简单、方便一些。 通过JDK内置了一些类、接口、关键字&#xff0c;补充完善了JDK对于并发编程支持的“短板”。 主要功能&#xff1a;&#xff08;1&am…

JUC

&#xff08;尚硅谷笔记&#xff09; Java JUC 简介  在 Java 5.0 提供了 java.util.concurrent &#xff08;简称 JUC &#xff09;包&#xff0c;在此包中增加了在并发编程中很常用 的实用工具类&#xff0c;用于定义类似于线程的自定义子 系统&#xff0c;包括线程池、异…

JUC基础知识(个人总结)

声明: 1. 本文为我的个人复习总结, 并非那种从零基础开始普及知识 内容详细全面, 言辞官方的文章 2. 由于是个人总结, 所以用最精简的话语来写文章 3. 若有错误不当之处, 请指出 一. 前置基础: IO 操作不占用 cpu, 只是我们一般拷贝文件使用的是【…

1、JUC概述

1.1 什么是JUC 在Java中&#xff0c;线程部分是一个重点&#xff0c;本篇文章说的JUC 也是关于线程的。JUC就是java.util .concurrent工具包的简称。这是一个处理线程的工具包&#xff0c;JDK1.5开始出现的。 1.2 线程和进程的概念 进程和线程 进程&#xff08;Process&…

JUC基础【万字篇】

JUC 1、什么是JUC JUC&#xff1a;指的是java.util三个并发编程工具包 java.util.concurrentjava.util.concurrent.atomicjava.util.concurrent.locks 实现多线程的四种方式&#xff1a; 继承Thread类实现Runnable接口实现Callable接口线程池 业务&#xff1a;普通的线程代…

java--JUC快速入门(彻底搞懂JUC)

java–JUC快速入门&#xff08;彻底搞懂JUC&#xff09; 文章目录 java--JUC快速入门&#xff08;彻底搞懂JUC&#xff09;1、学习多线程之前需要知道的一些概念。2、JUC的结构3、Lock锁(重点)4、集合类不安全5、Callable()6、常用的辅助类7、读写锁8、阻塞队列9、线程池 1、学…

Dbeaver做数据迁移

1、选择源头数据库的表、鼠标右击、选择导出数据 2、在数据转化弹框中&#xff0c;双击 ‘数据库&#xff0c;数据表’ 那一栏 3、选择目标数据库&#xff0c;调整字段类型映射关系 4、调整字段的映射关系 目前遇到的字段类型&#xff0c;只有 int&#xff0c;bigint 转 num…

dbeaver工具连接达梦数据库

、一 概述 DBeaver 是一个基于 Java 开发&#xff0c;免费开源的通用数据库管理和开发&#xff0c;DBeaver 采用 Eclipse 框架开发&#xff0c;支持插件扩展&#xff0c;并且提供了许多数据库管理工具&#xff1a;ER 图、数据导入/导出、数据库比较、模拟数据生成等&#xff0…

DBeaver 格式化sql

有时候我们拿到了一条sql语句是长长的&#xff0c;非常不容易阅读&#xff0c;这时我们就想说哪里可以格式下sql代码。 方法有很多种&#xff0c;这里我就用Dbeaver来格式化sql。 ①打开Dbeaver ②复制sql代码到SQL编辑器中&#xff0c;并选中 ③按ctrlshiftF&#xff0c;即…

【DBeaver】常用自定义设置

文章目录 背景一、用户界面设置1.1、22.3.4版本1.1.1、SQL编辑器-字体设置1.1.2、查询结果-字体设置 1.2、23.0.0版本1.2.1、应用字体&#xff08;导航栏等&#xff09;1.2.2、文本字体&#xff08;SQL输出、文本编辑器等&#xff09; 二、常规设置2.1、连接类型设置/环境设置 …

DBeaver导入Excel数据

目录 前言 导入准备 ​导入步骤 1.选中数据库表&#xff0c;右键&#xff0c;然后点击导入数据 2.双击CSV,选择待导入的文件 3.修改编码格式&#xff08;可选&#xff0c;不乱码不用&#xff09; 4.点击下一步&#xff0c;修改列的类型 5.一直下一步&#xff0c;点击…

Dbeaver基本使用

1&#xff1a;与plsql相比&#xff0c;Dbeaver没有右击直接查看表注释的功能&#xff0c;但是Dbeaver提供了一个“打开声明”的功能&#xff0c;里面可以查看一些比较实用的内容&#xff1a;表列注释、创建该表的create语句&#xff1a; 2&#xff1a;在一般开发的情况下&#…

【大数据】Hive可视化工具dbeaver

Hive可视化工具dbeaver 1、dbeaver基本介绍 dbeaver是一个图形化的界面工具&#xff0c;专门用于与各种数据库的集成&#xff0c;通过dbeaver我们可以与各种数据库进行集成通过图形化界面的方式来操作我们的数据库与数据库表&#xff0c;类似于我们的sqlyog或者navicat。 2、…

DBeaver安装及使用手册

一、DBeaver安装 1、在[DBeaver官网](https://dbeaver.io/download)进行数据库工具下载&#xff0c;下载好后双击运行2、选择语言后&#xff0c;点击OK 3、点击下一步 4、接受许可 5、选择可使用者&#xff0c;然后点击下一步 6、选择组件&#xff0c;一般选择默认即可 7…

DBeaver-Driver-All ( DBeaver驱动包,所有JDBC驱动整合包)

DBeaver-Driver-All DBeaver-Driver-All ( DBeaver驱动包 )整合所有DBeaver的JDBC驱动包&#xff0c;供DBeaver使用&#xff0c;无需每次都搜索和下载&#xff0c;只需clone本项目即可&#xff0c;一个包包含几乎所有的驱动&#xff0c;如果有缺漏的驱动欢迎提Issue补充。 DBe…

DBeaver 下载安装

1 下载地址(我下载的 Windows版本&#xff0c;根据系统需要选择版本) Releases dbeaver/dbeaver GitHubFree universal database tool and SQL client. Contribute to dbeaver/dbeaver development by creating an account on GitHub.https://github.com/dbeaver/dbeaver/rel…

DBeaver 安装

DBeaver 目录 DBeaver1、介绍2、发展史3、版本介绍4、下载与安装5、DBeaver 连接数据库&#xff08;MySql&#xff09;6、DBeaver 连接数据库&#xff08;Hive&#xff09;7、DBeaver 功能简介 1、介绍 DBeaver是一种通用数据库管理工具&#xff0c;适用于需要以专业方式使用数…

DBeaver驱动安装

最近打算用DBeaver它来查看SQLite文件&#xff0c;需要安装驱动&#xff0c;总是安装不上有点苦恼 下载驱动的时候可能会出现如下提示&#xff1a; Can’t create driver instance Error creating driver ‘SQLite’ instance. Most likely required jar files are missing. …

使用 DBeaver 导入数据

如何上传数据 在开始使用 DBeaver 之前&#xff0c;用户 以 excel 格式收集了有关电视剧的信息。他的文件已经包含所有必要的列&#xff0c;但我们的英雄不想将其中一些列传输到数据库&#xff0c;因为它们是为他个人使用而创建的&#xff0c;与站点访问者无关。要仅快速加载网…