并发和并行

简介: 并发和并行


并发编程:并发和并行

1 并发

并发是指多线程操作同一个资源

cpu单核,模拟出来多条线程,快速交替

2 并行

多个人一起行走

cpu多核,多个线程可以同时执行 线程池

并发编程的本质`:充分利用cpu资源

 题外话 如何查看cpu有多少核(处理器)?


image.png


可以右键电脑 -->管理找到截图的位置 查看具体的信息


image.png


用代码实现查看cpu有多少个处理器


package com.wyh.demo;
/**
 * @program: JUC
 * @description: 测试
 * @author: 魏一鹤
 * @createDate: 2022-02-10 21:24
 **/
public class demo01 {
public static void main(String[] args){
//查看cpu有多少核(处理器)
//cpu密执行 io密执行
        System.out.println(Runtime.getRuntime().availableProcessors());
    }
}


image.png


  1. 回顾多线程

线程有几个状态?

6个 分别是 新建 就绪 等待 执行 阻塞 死亡

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


wait和sleep的区别

企业中一般不会用sleep来休眠,而是使用TimeUnit(TimeUnit也是JUC下的)进行操作,相关代码如下,需要抛出异常(InterruptedException )


package com.wyh.demo;
import java.util.concurrent.TimeUnit;
/**
 * @program: JUC
 * @description: 测试
 * @author: 魏一鹤
 * @createDate: 2022-02-10 21:24
 **/
public class demo01 {
public static void main(String[] args) throws InterruptedException {
        //TimeUnit也是JUC下的
//睡一天
        TimeUnit.DAYS.sleep(1);
//睡两秒
        TimeUnit.SECONDS.sleep(2);
    }
}


1 来自不同的类 wait属于Object类的方法,sleep是Thread类的方法

2 关于锁的释放,wait会释放锁,sleep睡觉了,抱着锁睡觉,所以不会释放锁

3 使用的范围是不同的,wait必须在同步代码块中使用,sleep可以在任何地方使用

4 是否可以捕获异常,wait不需要捕获异常,sleep必须要捕获异常

目录
相关文章
|
5月前
|
存储 人工智能 自然语言处理
2026 实测多AI工具协同:项目上下文统一管理落地指南
本文分享2026年实测多AI工具协同经验,提出“协同底座+专业Agent”分工模式:外部AI专注单点任务(如代码生成、日志分析),底座统一管理项目上下文、权限、版本与链路追踪。已在研报生产、代码变更、故障排障、多语言内容四大场景落地,效率提升显著,接入成本低。
|
5月前
|
存储 人工智能 数据库
Agentforce 实战示例指南
通过四个完整的实战示例深入学习 Agentforce 开发:使用自定义 Apex 动作处理复杂产品查询、用 Agent Script 构建客户支持 Agent、用 Knowledge 和 Flow 为关联 Agent 注入最新术语、以及使用步骤变量强制执行多轮对话中的子代理顺序。每个示例都提供完整的端到端步骤说明。
330 0
Agentforce 实战示例指南
|
人工智能 自然语言处理 架构师
智能体来了!黎跃春老师经验拆解IP操盘手的AI企业赋能实践
黎跃春老师融合IP操盘与智能体技术,提出“技术是1,营销是0”的核心理念,依托阿里云平台实现AI赋能企业。通过“工具提效、人创价值”路径,助力IP操盘手构建可落地的全员营销智能体,推动从流量运营到企业服务的价值升级。
596 6
|
数据采集 人工智能 数据可视化
SpatialLM:手机视频秒建3D场景!开源空间认知模型颠覆机器人训练
SpatialLM 是群核科技开源的空间理解多模态模型,能够通过普通手机拍摄的视频重建 3D 场景布局,赋予机器人类似人类的空间认知能力,适用于具身智能训练、自动导航、AR/VR 等领域。
1690 5
SpatialLM:手机视频秒建3D场景!开源空间认知模型颠覆机器人训练
|
安全 网络安全 Android开发
APP-国内主流安卓商店-应用市场-鸿蒙商店上架之必备前提·全国公安安全信息评估报告如何申请-需要安全评估报告的资料是哪些-优雅草卓伊凡全程操作
APP-国内主流安卓商店-应用市场-鸿蒙商店上架之必备前提·全国公安安全信息评估报告如何申请-需要安全评估报告的资料是哪些-优雅草卓伊凡全程操作
3188 11
APP-国内主流安卓商店-应用市场-鸿蒙商店上架之必备前提·全国公安安全信息评估报告如何申请-需要安全评估报告的资料是哪些-优雅草卓伊凡全程操作
|
存储 架构师 Java
内存溢出原因与解决方案(4大主流方案详解)
本文详解内存溢出(OOM)的原因及解决方案。关注【mikechen的互联网架构】,10年+BAT架构经验倾囊相授。
内存溢出原因与解决方案(4大主流方案详解)
|
Kubernetes API Docker
kubernetes Operator 【2】实战CRD编程
kubernetes Operator 【2】实战CRD编程
kubernetes Operator 【2】实战CRD编程
|
Java
java基础 - 个人笔记
java基础 - 个人笔记
973 2
|
前端开发 IDE JavaScript
使用editorconfig配置你的编辑器
使用editorconfig配置你的编辑器
使用editorconfig配置你的编辑器