关于线程的讲解?

2016-02-19 17:06 2 1 收藏

下面这个关于线程的讲解?教程由图老师小编精心推荐选出,过程简单易学超容易上手,喜欢就要赶紧get起来哦!

【 tulaoshi.com - 编程语言 】

  Thread Scheduling

  In Java technology,threads are usually preemptive,but not necessarily Time-sliced(the process of giving each thread an equal amount of CPU time).It is common mistake to believe that "preemptive" is a fancy word for "does time-slicing".

  For the runtime on a Solaris Operating Environment platform,Java technology does not preempt threads of the same priority.However,the runtime on Microsoft Windows platforms uses time-slicing,so it preempts threads of the same priority and even threads of higher priority.Preemption is not guaranteed;however,most JVM implementations result in behavior that appears to be strictly preemptive.Across JVM implementations,there is no absolute guarantee of preemption or time-slicing.The only guarantees lie in the coder’s use of wait and sleep.

  The model of a preemptive scheduler is that many threads might be runnable,but only one thread is actually running.This thread continues to run until it ceases to be runnable or another thread of higher priority becomes runnable.In the latter case,the lower priority thread is preempted by the thread of higher priority,which gets a chance to run instead.

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  A thread might cease to runnable (that is,because blocked) for a variety of reasons.The thread’s code can execute a Thread.sleep() call,deliberately asking the thread to pause for a fixed period of time.The thread might have to wait to access a resource and cannot continue until that resource become available.

  All thread that are runnable are kept in pools according to priority.When a blocked thread becomes runnable,it is placed back into the appropriate runnable pool.Threads from the highest priority nonempty pool are given CPU time.

  The last sentence is worded loosed because:

  (1) In most JVM implementations,priorities seem to work in a preemptive manner,although there is no guarantee that priorities have any meaning at all;

  (2) Microsoft Window’s values affect thread behavior so that it is possible that a Java Priority 4 thread might be running,in spite of the fact that a runnable Java Priority 5 thread is waiting for the CPU.

  In reality,many JVMs implement pool as queues,but this is not guaranteed hehavior.

  线程调度(试翻译,欢迎指正)

  在java技术中,线程通常是抢占式的而不需要时间片分配进程(分配给每个线程相等的cpu时间的进程)。一个经常犯的错误是认为抢占就是分配时间片。

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  在Solaris平台上的运行环境中,相同优先级的线程不能相互抢占对方的cpu时间。但是,在使用时间片的windows平台运行环境中,可以抢占相同甚至更高优先级的线程的cpu时间。抢占并不是绝对的,可是大多数的JVM的实现结果在行为上表现出了严格的抢占。纵观JVM的实现,并没有绝对的抢占或是时间片,而是依赖于编码者对wait和sleep这两个方法的使用。

  抢占式调度模型就是许多线程属于可以运行状态(等待状态),但实际上只有一个线程在运行。该线程一直运行到它终止进入可运行状态(等待状态)或是另一个具有更高优先级的线程变成可运行状态。在后一种情况下,底优先级的线程被高优先级的线程抢占,高优先级的线程获得运行的机会。

  线程可以因为各种各样的原因终止并进入可运行状态(因为堵塞)。例如,线程的代码可以在适当时候执行Thread.sleep()方法,故意让线程中止;线程可能为了访问资源而不得不等待直到该资源可用为止。

  所有可运行的线程根据优先级保持在不同的池中。一旦被堵塞的线程进入可运行状态,它将会被放回适当的可运行池中。非空最高优先级的池中的线程将获得cpu时间。

  最后一个句子是不精确的,因为:

  (1)在大多数的JVM实现中,虽然不能保证说优先级有任何意义,但优先级看起来象是用抢占方式工作。

  (2)微软windows的评价影响线程的行为,以至尽管一个处于可运行状态的优先级为5的java线程正在等待cpu时间,但是一个优先级为4的java线程却可能正在运行。

  实际上,许多JVM用队列来实现池,但没有保证行为。

来源:https://www.tulaoshi.com/n/20160219/1614183.html

延伸阅读
标签: Web开发
字体(Font)的设置是网页制作新手遇到的第一个难点。如何控制字体大小,如何取消超链接字体的下划线是网友来信问得最多的。好,我们来彻底研究一下字体的各个方面: 字符集的设定。 在查看html文件原代码时,我们经常可以在文件头head和/head之间看到这么一句代码: meta http-equiv="Content-Type" content="text/html; charse...
一、run()和start() 这两个方法应该都比较熟悉,把需要并行处理的代码放在run()方法中,start()方法启动线程将自动调用 run()方法,这是由Java的内存机制规定的。并且run()方法必须是public访问权限,返回值类型为void。 二、关键字Synchronized 这个关键字用于保护共享数据,当然前提是要分清哪些数据是共享数据。每个对象都有一个锁...
标签: PS PS教程
友情提示:看不清文字请点击图片看大图 一、透视的基本概念 1.1、请不要相信自己的眼睛 1.2、媒婆、政客和画家 二、透视的原理 2.1、虚拟画面的建立 2.2、透视的映射原理 [next] 三、透视常用名词解释 3.1、视平线 3.2、视点和视距 3.3、灭线 3.4、心点 [next] 3.5、天点 3.6、地点 ...
第 3 部分 内容: 条件变量 pthread_cond_wait() 小测验 初始化和清除 等待 发送信号和广播 工作组 队列 data_control 代码 调试时间 工作组代码 代码初排 有关清除的注重事项 创建工作 threadfunc() join_threads() 结束语 参考资...
大家都知道,在开发过程中应该尽可能减少用户等待时间,让程序尽可能快的完成运算。可是无论是哪种语言开发的程序最终往往转换成汇编语言进而解释成机器码来执行。但是机器码是按顺序执行的,一个复杂的多步操作只能一步步按顺序逐个执行。改变这种状况可以从两个角度出发:对于单核处理器,可以将多个步骤放到不同的线程,这样一来用户完成UI...

经验教程

845

收藏

26
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部