这里解释一下线程的执行顺序?
从下面的代码中,我认为“y”会在x之前打印出来,但是“x”首先开始打印。我正在以线性方式阅读此内容,那么为什么“x”首先开始打印,即使 t.Start() 在主方法中首先被调用?
static void Main()
{
Thread t = new Thread(ThreadTest.WriteY);
t.Start();
for(int i = 0; i < 1000; i++) Console.Write("x");
}
public class ThreadTest
{
public static void WriteY()
{
for(int i = 0; i < 1000; i++) Console.WriteLine("y");
}
}
From the code below, I though thhat "y" would be printed out before x, but "x" starts printing first. I am reading this in a linear fashion, so why does "x" start printing first even though t.Start() is called first in the main mehod?
static void Main()
{
Thread t = new Thread(ThreadTest.WriteY);
t.Start();
for(int i = 0; i < 1000; i++) Console.Write("x");
}
public class ThreadTest
{
public static void WriteY()
{
for(int i = 0; i < 1000; i++) Console.WriteLine("y");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
好吧,这样考虑吧。
给你的朋友一叠文件,你自己也保留一叠。
然后你告诉你的朋友“开始在那些纸上写下数字”,然后你立即开始做同样的事情。
现在告诉我,你们谁会先完成?
而且,考虑到这是你的问题,为什么?
在这里不可能确定地输入正确答案,因为有很多因素会影响你们两个的速度:
所以基本上,你的朋友可能会在你之前开始设法开始,反之亦然,但你无法事先知道,这不是“正确的答案”。
每次运行该程序时,它都有可能表现不同,至少在两个线程之间的执行顺序方面是如此。
话虽如此,启动新线程会产生开销,这可能会导致主线程在额外线程之前进入循环。然而,如果相反的情况不可能发生,我会感到惊讶。
Well, consider it this way.
Give your friend a stack of papers, and keep a stack yourself.
Then you tell your friend "start writing out numbers on those papers", and then immediately you start doing the same.
Now tell me, who of you will finish first?
And, considering this is your question, why?
It is impossible to deterministically type out a correct answer here because there are so many things impacting how fast you two can:
So basically, your friend might end up starting before you manage to start, or vice versa, but you cannot know beforehand, and it isn't "the right answer".
Every time you run this program, it has the chance of behaving differently, at least in terms of execution order between the two threads.
Having said that, there is overhead involved in spinning up a new thread, which might tip the scale in favor of the main thread entering the loop before the extra thread does. However, I'd be surprised if there is no way that the opposite might happen.
当您生成一个新线程时,操作系统将接管并安排稍后进行的工作。关键是您无法知道它何时发生,这就是为什么您多次运行同一个应用程序并且每次都会看到不同的结果。
When you spawn off a new thread, the Operating System takes over and schedules the work to happen at a later time. The point is that you have no way to know when it'll take place, which is why you an run the same app multiple times and see different results every time.
启动一个新线程可能需要相当长的时间,因此
WriteY
需要一段时间才能运行。与此同时,主线程将继续运行,因此在大多数情况下(如果不是全部情况下),您都会看到大量 x 被打印。此外,调度本身由操作系统处理。
简而言之:您不应该尝试根据阅读源代码来猜测执行顺序。
Starting a new thread may require quite some time, so it will take a while before
WriteY
gets to run. In the meantime the main thread will continue to run and thus you will see a lot ofx
s printed in most if not all cases.Furthermore the scheduling itself is handled by the OS.
In short: You shouldn't try to guess the order of execution based on reading the source code.
仅仅因为您在某个时间启动一个线程并不意味着该线程中的代码将在调用方法中的下一行代码执行之前启动。
我猜想您可以运行该程序数千次,并最终得到一些以“y”开头的程序和其他以“x”开头的程序。
Just because you start a thread at a certain time doesn't mean that the code in that thread is going to start before the execution of the next line of code in the calling method.
I would guess that you could run the program thousands of times and wind up with some that start with 'y' and others that start with 'x'.
在这种情况下,线程执行顺序是不可预测的,
这可能是构建线程(堆栈帧/等)的代码导致速度减慢
thread execution order is unpredictable
in this case it will probably be the code that builds the thread (stackframe/etc) that is slowing things down
因为不能确定什么先开始。线程的执行没有确定的顺序。如果您启动程序 n 次,则可以假设您首先看到 x 和 y 都被打印。
如果您想确保线程执行的顺序,您应该查看 Thread.Join
Because it isn't assured what starts first. There isn't a deterministic order in which threads are executed. If you start your program n times, it can be assumed that you see both x and y printed first.
If you want to ensure an order in the way threads are executed you should look at Thread.Join