Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello every one can anybody tell what is the difference between Thread.sleep() and Thread.join()
Here i am doing some simple application
C#
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
Posted

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
 
Share this answer
 
v3
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.
 
Share this answer
 
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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900