JAVA语言多线程编程基础之基本线程机制、线程调度器、线程状态讲解
小标 2019-01-09 来源 : 阅读 1028 评论 0

摘要:本文主要向大家介绍了JAVA语言多线程编程基础之基本线程机制、线程调度器、线程状态讲解,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

本文主要向大家介绍了JAVA语言多线程编程基础之基本线程机制、线程调度器、线程状态讲解,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

一、基本的线程机制


并发编程使我们可以讲程序划分为多个分离的、独立运行的任务。通过使用多线程机制,这些独立任务(也被称为子任务)中的每一个都将由执行线程来驱动。一个线程就是在进程中的一个单一的顺序控制流,因此,单个进程可以拥有多个并发执行的任务,但是你的程序使得每个任务都好像有自己的CPU一样。其底层机制是切分CPU时间,但是你不需要考虑它。


线程模型为变成带来了便利,它简化了在单一程序中同时交织在一起的多个操作的处理。在使用线程时,CPU将轮流给每个任务二分配其占用时间(当系统使用时间切片机制时,情况确实如此。Solaris使用了FIFO并发模型:除非有高优先级的线程被唤醒,否则当前线程将一直运行,直至当前线程将一直运行,直到它被阻塞或者终止。这意味着具有相同优先级的其他线程在当前线程放弃处理器之前,将不会运行)。每个任务都觉得自己在一直占用CPU,但是事实上CPU时间是划分成片段分配给所有的任务(例外情况是程序确实运行在多个CPU之上)。线程的一大好处是可以使你从这个层次抽身出来,即使代码不必知道它是运行在具有一个还是多个CPU的机器上。所以,使用线程机制是一种建立透明的,可扩展的程序的方法,如果程序运行的太慢,为机器增添一个CPU就能很容易的加快程序的运行速度。多任务和多线程往往是使用多处理系统的最合理方式。


二、线程调度器


操作系统的核心,它实际就是一个常驻内存的程序,不断地对线程队列进行扫描,利用特定的算法(时间片轮转法、优先级调度法、多级反馈队列调度法等)找出比当前占有CPU的线程更有CPU使用权的线程,并从之前的线程中收回处理器,再使待运行的线程占用处理器。


上述描述很简单,虽然很抽象但至少大概能想象一下线程调度器充当什么角色。


线程调度器是一个操作系统服务,它负责为Runnable状态的线程分配CPU时间。一旦创建一个线程并启动它,它的执行便依赖于线程调度器的实现。时间分片是指将可用的CPU时间分配给可用的Runnable线程的过程。分配CPU时间可以基于线程优先级或者线程等待的时间。线程调度并不受到Java虚拟机控制,所以由应用程序来控制它是更好的选择(也就是说不要让的程序依赖于线程的优先级)。


上述有一些东西我并不赞同,但是看起来好像是这么回事。


三、线程状态


通过Thread类源码来看一下线程的状态


/**


* A thread state. A thread can be in one of the following states:


*


*


{@link #NEW}


* A thread that has not yet started is in this state. *


*


{@link #RUNNABLE}


* A thread executing in the Java virtual machine is in this state. *


*


{@link #BLOCKED}


* A thread that is blocked waiting for a monitor lock * is in this state. *


*


{@link #WAITING}


* A thread that is waiting indefinitely for another thread to * perform a particular action is in this state. *


*


{@link #TIMED_WAITING}


* A thread that is waiting for another thread to perform an action * for up to a specified waiting time is in this state. *


*


{@link #TERMINATED}


* A thread that has exited is in this state. *


*


* *


* A thread can be in only one state at a given point in time. * These states are virtual machine states which do not reflect * any operating system thread states. * * @since 1.5 * @see #getState */ public enum State { /** * 一个刚创建而为启动的线程处于该状态。由于一个线程实例只能够被启动一次。因此一个线程只可能又一次处于该状态 */ /** * Thread state for a thread which has not yet started. */ NEW, /** * 该状态可以看成是一个符合的状态。它包括两个子状态:READY和RUNNING。前者标识处于该状态的线程可以被JVM的 * 线程调度器(Schedule)进行调度而使之处于Running状态。后者表示处于该状态的线程正在运行,即响应线程对象的 * run方法中的代码所对应的指令正在由CPU执行。当Thread实例的yield方法被调用时或者由于线程调度器的原因, * 相应线程的状态会由RUNNING装换为READY。 */ /** * 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, /** * 一个线程发起一个阻塞时I/O(Blocking I/O)操作后,或者试图去获得一个由其他线程持有的锁时,相应的线程 * 会处于该状态。处于该状态的线程并不会占用CPU资源。当相应的锁被其他线程释放后,该线程的状态又可以转换为 * 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, /** * 一个线程执行了某些方法调用之后就会处于这种无限等待其他线程执行特定操作的状态。这些方法包括: * Object.wait()\Thread.join()\LockSupport.park()。能够使相应的线程从WAITING转换为RUNNING的相应方法 * 包括:Object.notify()\Object.notifyAll()\LockSupport.unpark(thread). */ /** * Thread state for a waiting thread. * A thread is in the waiting state due to calling one of the * following methods: *


*


{@link Object#wait() Object.wait} with no timeout


*


{@link #join() Thread.join} with no timeout


*


{@link LockSupport#park() LockSupport.park}


*


* *


A thread in the waiting state is waiting for another thread to * perform a particular action. * * For example, a thread that has called Object.wait() * on an object is waiting for another thread to call * Object.notify() or Object.notifyAll() on * that object. A thread that has called Thread.join() * is waiting for a specified thread to terminate. */ WAITING, /** * 该状态和WAITING类似,差别在于处于该状态的线程并非无限的等待其他线程执行特定操作,而是处于带有时间限制的 * 等待状态。当其他线程没有在指定时间内执行该线程所期望的特定操作时,该线程的状态自动切换为RUNNABLE。 */ /** * 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: *


*


{@link #sleep Thread.sleep}


*


{@link Object#wait(long) Object.wait} with timeout


*


{@link #join(long) Thread.join} with timeout


*


{@link LockSupport#parkNanos LockSupport.parkNanos}


*


{@link LockSupport#parkUntil LockSupport.parkUntil}


*


*/ TIMED_WAITING, /** * 已经执行结束的线程处于该状态。由于一个线程实例只能够被启动一次,因此一个线程也只可能又一次处于该状态。 * Thread实例的run方法正常返回或者由于抛出异常而提前终止都会导致相应线程处于该状态。 */ /** * Thread state for a terminated thread. * The thread has completed execution. */ TERMINATED; }


          

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程