首页 / 知识
JUC底层的详细学习
2023-04-11 16:23:00

1、什么是JUC
源码+官方文档
JUC是 java util concurrent
面试高频问JUC——!
java.util 是Java的一个工具包——
业务:普通的线程代码 Thread
Runnable: 没有返回值、效率相比于Callable 相对较低!
2、线程和进程
进程:一个程序,QQ.EXE Music.EXE;数据+代码+pcb
一个进程可以包含多个线程,至少包含一个线程!
Java默认有几个线程?2个线程! main线程、GC线程
线程:开了一个进程Typora,写字,等待几分钟会进行自动保存(线程负责的)
对于Java而言:Thread、Runable、Callable进行开启线程的,我们之前。
提问?JAVA真的可以开启线程吗? 开不了的!
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 then
it will be passed up the call stack */
}
}
}
//这是一个C++底层,Java是没有权限操作底层硬件的
private native void start0();
Java是没有权限去开启线程、操作硬件的,这是一个native的一个本地方法,它调用的底层的C++代码。
并发、并行
并发: 多线程操作同一个资源。
CPU 只有一核,模拟出来多条线程,天下武功,唯快不破。那么我们就可以使用CPU快速交替,来模拟多线程。
并行: 多个人一起行走
CPU多核,多个线程可以同时执行。 我们可以使用线程池!
public class Test1 {
public static void main(String[] args) {
//获取cpu的核数
System.out.println(Runtime.getRuntime()。availableProcessors());
}
}
并发编程的本质:充分利用CPU的资源!
线程有几个状态?
线程的状态:6个状态
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;
}
|
最新内容
相关内容
Python的字典排序
Python的字典排序,代码,数据,培训,字典,函数,表达式,内容,列表,排列,问题,字典是Python语言中的一种数据结构,每一个字典元素是由一对key-valupython的调用绑定方法和非绑定方法
python的调用绑定方法和非绑定方法,代码,方法,实例,第一,培训,时计,奇数,偶数,参数,定义,在Python中,如果用实例去调用方法,这种限制就被称为PyPython的经典题目
Python的经典题目,数字,数据,公司,培训,星期六,星期,字母,水仙花,次方,偶数,1、水仙花数用python打印出100-999所有的水仙花数,所谓水仙花数是python调试的几种方式
python调试的几种方式,代码,位置,信息,状态,培训,数据,分析,变量,函数,方式,python作为一种脚本语言,很多时候我们习惯于它的简洁,习惯于它的修为何你的Python代码应是扁平与稀疏
为何你的Python代码应是扁平与稀疏的,代码,培训,信息,观察,设计,工具,嵌套,闻闻,程序员,沉思,Python之禅之所以得名,正是由于它那简明扼要的规python的应用领域
python的应用领域,数据,分析,网络,工作,代码,人工智能,项目,金融,量化交易,业务,应用领域1:人工智能Python语言是目前公认学习人工智能的基础用Python开发一个简单的猜数字游戏
用Python开发一个简单的猜数字游戏,数字,代码,培训,官网,设备,程序,玩家,注释,内容,游戏,本文介绍如何使用Python制作一个简单的猜数字游戏。Python之关于高效使用字典的清单
Python之关于高效使用字典的清单,代码,数据,字典,培训,扩大,时报,方式,方法,对象,列表,字典(dict)对象是Python最常用的数据结构,社区曾有人开Python与c#的区别
Python与c#的区别,代码,平台,名称,培训,系统,设计,技术,标准,脚本,变量,现在来看下c#。它们的技术差异很大,但都适用于web开发。Python对c#的Python的英语搭配助手应用开发研究
Python的英语搭配助手应用开发研究,信息,设计,网站,在线,代码,环境,数据,分析,时间,培训,基于Python自带的GUI工具包Tkinter来实现窗口视窗设Python学习的三个阶段
Python学习的三个阶段,数据,基础,体系,基本知识,业务,传统行业,企业,培训,分析,语言,首先,在当前诸多的计算机编程语言当中,Python语言确实算是Python线程编程的两种方式
Python线程编程的两种方式,代码,包装,线程,培训,函数,对象,模块,参数,方式,方法,Python中如果要使用线程的话,python的lib中提供了两种方式。