thread.sleep和thread.join之间的混淆 [英] An confuse between thread.sleep and thread.join

查看:108
本文介绍了thread.sleep和thread.join之间的混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,谁能说出Thread.sleep()和Thread.join()有什么区别
在这里,我正在做一些简单的应用程序

Hello every one can anybody tell what is the difference between Thread.sleep() and Thread.join()
Here i am doing some simple application

static void Main(string[] args)
 {
 Thread th = new Thread(new ThreadStart(startAnew));
            th.Start();
 // th.Join(3000);
            Thread.Sleep(3000);
            Console.WriteLine("I will come later");
            Console.ReadLine();


}

public static void startAnew()
        {
Console.WriteLine("Its from thread");
            Thread.Sleep(5000);
            Console.WriteLine("end of the thread");
}



所以在这里我正在使用第一次睡眠并执行,然后我将注释睡眠并取消注释联接行并执行,但是我发现这两个执行之间没有区别



so here i am using 1st sleep and execute then i will comment the sleep and un-comment the Join line and execute but i find that there is no difference between this two execution

推荐答案

方法Thread.Sleep是静态方法.没有传递线程引用.那么,哪个线程正在睡眠?主叫人.

与此相反,方法Thread.Join是实例方法.您将实例引用传递给此方法.从方法实现的角度来看,实例作为"this"隐式参数传递.从调用者的角度来看,您传递了某个线程的实例,然后是调用线程.具有Thread thread = //...后,您将使用通常的语法thread.Join()传递thread的实例.此方法也是调用线程的阻塞方法,而不是传递给调用的线程thread. SleepJoin之间的唯一区别是唤醒调用线程的主要条件:在第一种情况下,这只是计时器到期,在第二种情况下,这是线程的终止thread.

在这两种情况下,调用线程都处于特殊的等待状态:操作系统将其关闭,并且从不调度回到执行状态(因此处于等待状态的线程不会浪费任何CPU时间),直到被唤醒为止.我提到了唤醒的主要(预期")条件.还有其他条件.对于Join(Int32)Join(TimeSpan),通过终止传递给方法的线程或终止时间(以先到者为准)来唤醒调用线程.同样,任何线程都将被AbortInterrupt
唤醒
请参阅:
http://msdn.microsoft.com/en-us/library/system.threading. thread.aspx [^ ].

一切都在这里得到了很好的解释,请仔细阅读.

像许多实验一样,您的无差异"观察结果无法证明任何事情.您只是没有遵循正确的逻辑.基于这些方法的知识,您的结果很容易解释.这很简单:在您的情况下,调用Join(Int32)的线程总是通过到期时间来唤醒,而永远不会通过终止线程th来唤醒,原因很简单:5000> 3000 .

—SA
The method Thread.Sleep is the static method. No thread reference is passed. So, which thread is sleeping? The calling one.

In contrast to this, the method Thread.Join is an instance method. You pass an instance reference to this method. From the view point of the method implementation, the instance is passed as "this", implicit parameter. From the stand point of the caller, you pass an instance of some thread, other then the calling thread. Having Thread thread = //..., you pass the instance of thread using the usual syntax thread.Join(). This method is a blocking method for a calling thread as well, not the thread thread passed to a call. The only difference between Sleep and Join is the primary condition for waking up of the calling thread: in first case, this is just timer expiration, in second case, this is the termination of thread thread.

In both cases, a calling thread in put in a special wait state: the OS switches it off and never schedule back to execution (so a thread in the wait state waste no CPU time) until it is awaken. I mentioned main ("intended") conditions of waking up. There are other conditions. For Join(Int32) or Join(TimeSpan) the calling thread is waken up either by termination of the thread passed to the method or expiration time, whichever comes first. Also, any thread will be awaken by Abort or Interrupt

Please see:
http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx[^].

Everything is well explained here, just read with attention.

Your "no difference" observation proves nothing, as many experiments. You just don''t follow proper logic. Your result is easy to explain based on the knowledge of what these methods do. This is simple: the thread calling Join(Int32) in your case is awakened always by expiration time and never by the termination of the thread th by a simple reason: 5000 > 3000.

—SA


arunrv,

join() sleep()之间有区别.
join()将等待,直到超时到期或线程完成.
sleep()只会等待指定的时间,除非被中断.

所以.完全有可能join()返回比指定时间快得多.

Netbeans警告您有关sleep()而不是join()的原因正是这种差异.

join()等待有意义的东西,而
sleep()只是坐在那里什么都不做.

如果您不等待某事,那为什么还要等待呢?它很少有意义,因此会发出警告.
Hi arunrv,

There is a difference between join() and sleep().
join() will wait until the timeout expires or the thread finishes.
sleep() will just wait for the specified amount of time unless interrupted.

So. it is perfectly possible for join() to return much faster than the specified time.

The reason why Netbeans warns you about sleep() and not about join() is precisely that difference.

join() waits for something meaningful while
sleep() just sits there doing nothing.

If you aren''t waiting for something, then why would you want to wait at all? It is rarely meaningful, hence the warning.


当然,由于您是在单线程环境中运行它,因此执行上肯定没有区别.

Thread.Join对于控制多线程环境中的应用程序流很有用.

Thread.Sleep 将休眠/暂停当前/主线程.
&
Thread.Join 将阻塞调用线程(通常是主线程),直到一个线程终止或经过指定的时间为止.
Surely there will be no difference in execution since you are running it in a single threaded environment.

Thread.Join is useful to control the application flow in a multi-threaded environment.

Thread.Sleep will Sleep/pause the Current/Main Thread.
&
Thread.Join will Block the calling thread(Usually the Main Thread) until a thread(s) terminates or the specified time elapses.


这篇关于thread.sleep和thread.join之间的混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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