线程基础知识01-Thread类,Runnable接口
常见面试题:创建一个线程的常用方法有哪些?Thread创建线程和Runnable创建线程有什么区别?
答案通常集中在,继承类和实现接口的差别上面;
如果深入问一些问题:1.要执行的任务写在run()方法中,为什么要用start()方法启动?等等问题
简单的问题还是可以回答一哈子,但是涉及到深入些的问题,就只能看看源码,才能更好的回答问题了:
1.为啥线程要用start()方法启动?
首先要从Thread类的源码入手:
- Thread类实现了Runnable接口
public
class Thread implements Runnable {
- 可以看出,Thread类里面有一个Runnable接口的元素,通过构造方法给Thread类里面的Runnable元素赋值;
private Runnable target;
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
private void init(ThreadGroup g, Runnable target, String name,
long stackSize, AccessControlContext acc,
boolean inheritThreadLocals) {
//省略部分代码
this.group = g;
this.daemon = parent.isDaemon();
this.priority = parent.getPriority();
//......省略部分代码
this.target = target;//注意这里的赋值操作
setPriority(priority);
if (inheritThreadLocals && parent.inheritableThreadLocals != null)
this.inheritableThreadLocals =
ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
/* Stash the specified stack size in case the VM cares */
this.stackSize = stackSize;
/* Set thread ID */
tid = nextThreadID();
}
- run方法很简单,就是判断是不是通过构造函数传入了Runnable接口,可以看出,线程的方法并没有执行;
@Override
public void run() {
if (target != null) {
target.run();
}
}
- start()方法中通过调用start0()执行Run()方法;类似于this.run();报错后也是忽略状态
public synchronized void start() {
if (threadStatus != 0)
throw new IllegalThreadStateException();
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 */
}
}
}
private native void start0();
- 可以借鉴的地方:
将任务封装在单独的接口中,可以多种实现,通过统一的方法去调用;
interface A {
run(){)
}
class B implements A {
private A a;
B(){}
B(A a){this.a = a}
run(){
if(a != null ){a.run}
}
start(){this.run()}
}
2.Thread的几种状态
- 类中给出的几种状态
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;
}
下面这个图能更好的理解线程的状态
(图片来自《java并发编程艺术》)
1.线程开启,存在两种情况,运行状态和临时阻塞状态(调用方法,但没有获取到锁的情况下);
运行状态,具有执行资格和执行权;临时阻塞状态,只有执行资格没有执行权;
2.线程运行完之后有两种状态:冻结/等待和TERMINATED,
冻结状态释放执行权和执行资格。调用wait()或sleep()方法
3.interceptor和stop之间的区别
stop:终结一个线程不会保证资源释放,比较暴力;
interceptor:线程被中断了,可以通过isInterceptor方法获取是否被终端;但是抛异常,或者isItercepter方法后,终端状态会被清楚;
比较好的线程终止方案,可以增加一个标识符eg:flag=true的判断,要中断的时候,将标识符设置位false即可;
4.什么是自旋锁?什么是无锁编程?
CAS简单了解: https://www.cnblogs.com/perferect/p/12761558.html
5.死锁
死锁的定义:一组互相竞争资源的线程因互相等待,导致“永久”阻塞的现象。
最常出现死锁的情况是,当两个或多个锁互相嵌套,一个锁内部进行其他锁的竞争;
class test{
private static final Object obj1 = new Object();
private static final Object obj2 = new Obbject();
public static void main(String[] args){
new Thread(){
public void run(){
synchronized(obj1){
Thread.sleep(100);
synchronized(obj2){
Thread.sleep(200);
}
}
}
}.start();
new Thread(){
public void run(){
synchronized(obj2){
Thread.sleep(100);
synchronized(obj1){
Thread.sleep(200);
}
}
}
}.start();
}
}
线程基础知识01-Thread类,Runnable接口的更多相关文章
- JAVA与多线程开发(线程基础、继承Thread类来定义自己的线程、实现Runnable接口来解决单继承局限性、控制多线程程并发)
实现线程并发有两种方式:1)继承Thread类:2)实现Runnable接口. 线程基础 1)程序.进程.线程:并行.并发. 2)线程生命周期:创建状态(new一个线程对象).就绪状态(调用该对象的s ...
- java 多线程:Thread类;Runnable接口
1,进程和线程的基本概念: 1.什么是进程: 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机 ...
- Java多线程01(Thread类、线程创建、线程池)
Java多线程(Thread类.线程创建.线程池) 第一章 多线程 1.1 多线程介绍 1.1.1 基本概念 进程:进程指正在运行的程序.确切的来说,当一个程序进入内存运行,即变成一个进程,进程是处于 ...
- java线程基础知识----线程基础知识
不知道从什么时候开始,学习知识变成了一个短期记忆的过程,总是容易忘记自己当初学懂的知识(fuck!),不知道是自己没有经常使用还是当初理解的不够深入.今天准备再对java的线程进行一下系统的学习,希望 ...
- Java并发之线程管理(线程基础知识)
因为书中涵盖的知识点比较全,所以就以书中的目录来学习和记录.当然,学习书中知识的时候自己的思考和实践是最重要的.说到线程,脑子里大概知道是个什么东西,但很多东西都还是懵懵懂懂,这是最可怕的.所以想着细 ...
- Java__线程---基础知识全面实战---坦克大战系列为例
今天想将自己去年自己编写的坦克大战的代码与大家分享一下,主要面向学习过java但对java运用并不是很熟悉的同学,该编程代码基本上涉及了java基础知识的各个方面,大家可以通过练习该程序对自己的jav ...
- Java线程基础知识(状态、共享与协作)
1.基础概念 CPU核心数和线程数的关系 核心数:线程数=1:1 ;使用了超线程技术后---> 1:2 CPU时间片轮转机制 又称RR调度,会导致上下文切换 什么是进程和线程 进程:程序运行资源 ...
- Java 线程--继承java.lang.Thread类实现线程
现实生活中的很多事情是同时进行的,Java中为了模拟这种状态,引入了线程机制.先来看线程的基本概念. 线程是指进程中的一个执行场景,也就是执行流程,进程和线程的区别: 1.每个进程是一个应用程序,都有 ...
- java线程基础知识----线程与锁
我们上一章已经谈到java线程的基础知识,我们学习了Thread的基础知识,今天我们开始学习java线程和锁. 1. 首先我们应该了解一下Object类的一些性质以其方法,首先我们知道Object类的 ...
随机推荐
- 转载:windows下安装mac虚拟机(Vmvare+mac)
体验Mac的高效与思想,每个技术人都应该去了解和体验,本文转载自网络,使用Vmvare,虚拟Mac系统 https://blog.csdn.net/qq_31867709/article/detail ...
- 【原创】Linux中断子系统(二)-通用框架处理
背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...
- 呀,葵花宝典![IT项目经理成长晋升记2]
走出办公室时,老吴让王小白认真看下公司的项目管理体系和质量管理体系培训材料.公司这几年连续通过了ISO质量体系认证,通过了CMMI3,已有一套完整的组织过程体系. 因为从投标开始,到公示,还有一周时间 ...
- Jackson乱码问题
在配置文件中加入下面的内容 <!-- Json乱码问题配置--> <mvc:annotation-driven> <mvc:message-converters regi ...
- 安装elasticsearch的坑
elasticsearch启动报“此时不应有 \Common 原因 Java 环境变量出错 解决 修改 elasticsearch.bat , 添加一句 : SET params='%*' SET J ...
- os:获取当前目录路径,上级目录路径,上上级目录路径
import os '''***获取当前目录***''' print(os.getcwd()) print(os.path.abspath(os.path.dirname(__file__))) '' ...
- git&github&Jenkins完成可持续集成
1.安装git :想要安装Git首先要下载Git的安装包程序. Git安装包下载地址:https://git-scm.com/downloads/ 2.双击下载git安装包进入安装界面, 点击下一步, ...
- Java中泛型的继承
最新在抽取公共方法的时候,遇到了需要使用泛型的情况,但是在搜索了一圈之后,发现大部分博客对于继承都说的不太清楚,所幸还有那么一两篇讲的清楚的,在这里自己标记下. 以我自己用到的代码举例,在父类中使用了 ...
- 从新冠疫情出发,漫谈 Gossip 协议
众所周知周知,疫情仍然在全球各地肆虐.据最新数据统计,截至北京时间 2020-05-28,全球累计确诊 5698703 例,累计死亡 352282 例,累计治愈 2415237 例. 从上面的统计数据 ...
- 循序渐进VUE+Element 前端应用开发(11)--- 图标的维护和使用
在VUE+Element 前端应用中,图标是必不可少点缀界面的元素,因此整合一些常用的图标是非常必要的,还好Element界面组件里面提供了很多常见的图标,不过数量不是很多,应该是300个左右吧,因此 ...