并发和并行

简介: 并发和并行


并发编程:并发和并行

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必须要捕获异常

目录
相关文章
|
4月前
并发与并行的区别(详细介绍)
并发与并行的区别(详细介绍)
677 0
|
6月前
并发和并行以及他们的区别
并发:         并发指的是多个任务交替执行的能力,这些任务可能不是同时执行,而是通过快速切换在不同任务之间来实现“同时执行”的效果。在多核处理器上,多个线程可以真正同时执行,而在单核处理器上,线程之间通过时间片轮转实现并发。         所以当谈论并发的时候一定要加个单位时间,也就是说单位时间内并发量是多少?离开了单位时间其实是没有意义的。 并行:         并行指的是多个任务同时执行的能力,每个任务都在独立的CPU上执行。并行通常用于同时处理独立任务,这些任务可以同时执行,而不需要相互等待或协同工作。 两者区别:         关键区别在于并发强调任务在时间上交替执行
40 0
|
1月前
|
调度 数据库 计算机视觉
并行和并发的区别(详细)
并行和并发的区别(详细)
|
3月前
|
机器学习/深度学习 分布式计算 负载均衡
并发与并行
并发与并行
31 0
|
8月前
|
并行计算 调度
多线程的并发和并行
多线程的并发和并行
69 0
|
6月前
并行,并发?
并行,并发?
15 0
|
7月前
|
SQL 开发框架 测试技术
常见的并发问题有哪些都不知道,还怎么说自己是大佬!!
常见的并发问题有哪些都不知道,还怎么说自己是大佬!!
80 0
|
9月前
|
存储 并行计算 安全
并发和并行的区别
并发和并行的区别
|
11月前
串行、并行和并发的区别
串行、并行和并发的区别
|
SQL 数据库
初识并发问题
初识并发问题
94 0
初识并发问题

热门文章

最新文章

相关实验场景

更多