有线程调度程序时,如何使执行线程并发运行? [英] How can threads of execution be running concurrently when there is a thread scheduler?

查看:84
本文介绍了有线程调度程序时,如何使执行线程并发运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我一直在阅读的定义:

From the definitions I've been reading:

线程基本上是并发运行的代码(在 相同时间).

threads are basically pieces of code that are running concurrently (at the same time).

但是,如何在存在线程调度程序的情况下同时运行它们?

However how can they be running concurrently with the existence of a thread scheduler?

我读到,线程调度程序基本上是从Runnable线程池中随机选择一个线程在某个时刻运行.从中我得到一个确切的时间点,只有一个可运行线程真正处于运行状态(运行). (所有这些都是来自SCJP Sun认证程序员学习指南.)有人可以澄清这一点吗?

I read that the thread scheduler basically chooses randomly a thread to run at a certain moment from the pool of Runnable threads. From that i got that at a precise point of time, only one runnable thread is truly in the run state(running). (all of this is from SCJP Sun Certified Programmer study guide) Can anyone clarify this?

这些线程真的同时运行吗?

推荐答案

但是如何在存在线程调度程序的情况下同时运行它们?

However how can they be running concurrently with the existence of a thread scheduler?

它们并不总是并发运行,调度程序的工作是交换正在运行的线程,以便它们看起来似乎并发运行.即太快了,您看不到.

They are not always running concurrently, the scheduler's job is to swap the running threads around so that they appear to be running concurrently. i.e. too fast for you to see.

调度程序使用的时间片为0.1毫秒.您只能看到10到25毫秒的闪烁,因此这太快了,您看不到,但是它正在快速交换线程,因此看起来是并发的.

The scheduler uses a time slice which is 0.1 ms. You can only see a flicker of 10 - 25 ms, so this is too fast for your to see, but it is quickly swapping threads so it appears there is concurrency.

例如您不会看到电影从一帧跳到下一帧.每帧每1/42秒改变一次,因此您认为在实际使用高速相机拍摄时会看到动静,屏幕看起来像跳动的.

e.g. you don't see movies jumping from one frame to the next. Each frame is changed every 1/42nd of a second so you think you see movement when actually to a high speed camera the screen would look jumpy.

如果您有一个逻辑CPU,则所有线程都将交换到一个CPU.如果您有多个逻辑CPU,则一小部分可以立即运行,其余的则需要等待.

If you have one logical CPU, all the thread are being swapped to one CPU. If you have multiple logical CPUs, a small set can be running at once and the rest have to wait.

这篇关于有线程调度程序时,如何使执行线程并发运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆