JUC概述
JUC概述1:
首先是进程和线程的概念:
进程:是指系统在系统中正在运行的一个应用程序,程序一旦运行就是进程,进程是资源分配的最小单位
线程:进程之内独立执行,是程序执行的最小单位
线程的六大状态:在线程的枚举类中
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 {@code Object.wait()}
* on an object is waiting for another thread to call
* {@code Object.notify()} or {@code Object.notifyAll()} on
* that object. A thread that has called {@code Thread.join()}
* 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;
}
| 状态名称 | 说明 |
|---|---|
| new | 初始状态 |
| runnable | 运行状态 |
| blocked | 阻塞状态 |
| waiting | 等待状态,一直等(不见不散) |
| time_waiting | 超时等待,(过时不候) |
| terminated | 终止状态 |
wait和sleep的区别:
- sleep是Thread的静态方法,wait是Object的方法,任何对象实例化都能调用
- sleep不会释放锁,他也不需要占用锁,wait会释放锁,但是调用它的前提是当前线程占有锁
- 它们都可以interrupted被中断
并发和并行:
并发是指多个事情在同一个时间段中执行
并行是指多个事情在同一时刻执行
管程:
是一种同步机制,保证同一时间内只有一个线程访问被保护数据或者代码
jvm同步基于进入(加锁)和退出(解锁),是管程对象实现的
大意就是进加锁,退是解锁,通过管程对象管理
用户线程:自定义线程 主线程结束了,用户线程还存在,则表示JVM还存在
public class demo {
public static void main(String[] args) {
Thread a = new Thread(() -> {
System.out.println(Thread.currentThread().getName()+"::"+Thread.currentThread().isDaemon());
while (true){}
}, "a");
a.start();
System.out.println(Thread.currentThread().getName());
}
}
守护线程:
ex:垃圾回收 没有用户线程了,都是守护线程,JVM结束
public class demo {
public static void main(String[] args) {
Thread a = new Thread(() -> {
System.out.println(Thread.currentThread().getName()+"::"+Thread.currentThread().isDaemon());
while (true){}
}, "a");
//设置子线程为守护线程
a.setDaemon(true);
a.start();
System.out.println(Thread.currentThread().getName());
}
}
JUC概述的更多相关文章
- JUC之文章整理以及汇总
JUC文章汇总 JUC部分将学习<JUC并发编程的艺术>和<尚硅谷-大厂必备技术之JUC并发编程>进行博客的整理,各文章中也会不断的完善和丰富. JUC概述 JUC的视频学习和 ...
- 001-多线程-JUC集合-框架概述
一.概述 1.1.java集合 java集合的架构,主体内容包括Collection集合和Map类:而Collection集合又可以划分为List(队列)和Set(集合). 1. List的实现类主要 ...
- Java多线程系列--“JUC锁”07之 LockSupport
概述 本章介绍JUC(java.util.concurrent)包中的LockSupport.内容包括:LockSupport介绍LockSupport函数列表LockSupport参考代码(基于JD ...
- JUC 之 ThreadPoolExecutor 的一些研究
ThreadPoolExecutor 概述:===================================================================== 构造函数: 4个 ...
- 多线程学习笔记二之JUC组件
目录 概述 JUC锁框架图 使用内置锁还是JUC显示锁? 概述 为了对共享资源提供更细粒度的同步控制,JDK5新增了java.util.concurrent(JUC)并发工具包,并发包新增了Loc ...
- JUC同步器框架
The java.util.concurrent Synchronizer Framework 前提 AQS(java.util.concurrent.locks.AbstractQueuedSync ...
- java并发编程(七)----(JUC)ReadWriteLock
前面我们已经分析过JUC包里面的Lock锁,ReentrantLock锁和semaphore信号量机制.Lock锁实现了比synchronized更灵活的锁机制,Reentrantlock是Lock的 ...
- 6.JUC之ReentrantReadWriteLock
一.概述: Java纪年1.5年,ReentrantReadWriteLock诞生于JUC,此后,国人一般称它为读写锁.人如其名,他就是一个可重入锁,同时他还是一个读写锁 a)跟ReentrantLo ...
- 005-多线程-锁-JUC锁-LockSupport【使用、Unsafe、对比Object的wait、底层源码】
一.概述 在Java多线程中,当需要阻塞或者唤醒一个线程时,都会使用LockSupport工具类来完成相应的工作.LockSupport定义了一组公共静态方法,这些方法提供了最基本的线程阻塞和唤醒功能 ...
随机推荐
- Python基础(map/reduce)
from functools import reduce#reduce函数在python3的内建函数移除了,放入了functools模块 #map() list1 = [1,2,3,4,5,6,7,8 ...
- Django笔记&教程 6-3 使用模型(models)创建表单(form)
Django 自学笔记兼学习教程第6章第3节--使用模型(models)创建表单(form) 点击查看教程总目录 本文参考:Forms for models 1 - 初步介绍 很多时候,我们使用的表单 ...
- 如何用webgl(three.js)搭建一个3D库房,3D密集架,3D档案室(升级版)
很长一段时间没有写3D库房,3D密集架相关的效果文章了,刚好最近有相关项目落地,索性总结一下 与之前我写的3D库房密集架文章<如何用webgl(three.js)搭建一个3D库房,3D密集架,3 ...
- C++内存管理剖析
C++内存管理 C++中有四种内存分配.释放方式: 最高级的是std::allocator,对应的释放方式是std::deallocate,可以自由设计来搭配任何容器:new/delete系列是C++ ...
- printf("%d\n",printf("%d",printf("%d",i)));
#include <stdio.h> int printf( const char *format, ... );首先 得看printf的返回类型是 int 这个函数的返回值是 你输出的位 ...
- [noi1760]SAM
建立SAM,求出每一个节点最左边的出现位置(即right集合中的最小元素,在树上dfs即可) 枚举左端点i和右端点j(保证j是最小的满足$s[i,j)$不是$s[0,i)$的子串),维护k表示$s[i ...
- 从零开始学Kotlin第一课
Kotlin的方法: 一个简单的计算器: fun main(args:Array<String>){ //主函数main方法 var a=8; var b=9; println(plus( ...
- Python+selenium之键盘和鼠标事件
- 使用protobuf-java-format包 JsonFormat转Json部分默认值字段消失问题
使用protobuf-java-format包 JsonFormat转Json部分默认值字段消失问题 1.产生的bug XXXXXXXXRequest.Builder request = XXXXXX ...
- 洛谷 P7451 - [THUSCH2017] 杜老师(线性基+根分+结论题)
题面传送门 看到乘积为平方数我们可以很自然地想到这道题,具体来说,我们对 \(1\sim 10^7\) 中所有质因子标号 \(1,2,\cdots,\pi(10^7)\),对于 \(x\in[l,r] ...