并发编程:并发和并行
1 并发
并发是指多线程操作同一个资源
cpu单核,模拟出来多条线程,快速交替
2 并行
多个人一起行走
cpu多核,多个线程可以同时执行 线程池
并发编程的本质`:充分利用cpu资源
题外话 如何查看cpu有多少核(处理器)?
可以右键电脑 -->管理找到截图的位置 查看具体的信息
用代码实现查看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()); } }
- 回顾多线程
线程有几个状态?
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必须要捕获异常