Synchronous threading will execute each thread/command in the order they have been created/started
Suppose there are 4 threads A, B, C, D
Application Starts at 10:00:00
time: 10:00:00: -> Start thread A (will run for 5 seconds)
time: 10:00:05: -> Start thread B (will run for 10 seconds)
time: 10:00:15: -> Start Thread B1 (will run for 5 seconds)
time: 10:00:20: -> Start thread C (will run for 15 seconds)
end application: total run time will be 35 seconds
Asynchronous threading will run the threads at the same time/ parallel processing, or we can say it will take the thread that is available at the earliest.
Application Starts at 10:00:00
time: 10:00:00: -> Start thread A (will run for 5 seconds) / Start thread B (will run for 10 seconds) / Start thread C (will run for 15 seconds)
time: 10:00:10: -> Start Thread B1 (will run for 5 seconds)
end application: total run-time will be 15 seconds (because the longest run-time is thread C)
So Async Threading save time, effective as well as well.
Example:-
Thanks, I hope you understand the basics of Async Threading.
Suppose there are 4 threads A, B, C, D
Application Starts at 10:00:00
time: 10:00:00: -> Start thread A (will run for 5 seconds)
time: 10:00:05: -> Start thread B (will run for 10 seconds)
time: 10:00:15: -> Start Thread B1 (will run for 5 seconds)
time: 10:00:20: -> Start thread C (will run for 15 seconds)
end application: total run time will be 35 seconds
Asynchronous threading will run the threads at the same time/ parallel processing, or we can say it will take the thread that is available at the earliest.
Application Starts at 10:00:00
time: 10:00:00: -> Start thread A (will run for 5 seconds) / Start thread B (will run for 10 seconds) / Start thread C (will run for 15 seconds)
time: 10:00:10: -> Start Thread B1 (will run for 5 seconds)
end application: total run-time will be 15 seconds (because the longest run-time is thread C)
So Async Threading save time, effective as well as well.
Example:-
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading;
using
System.Net;
namespace
thread_asynchronous
{
class
Program
{
static
void
Main(string[]
args)
{
DownloadAynchronously();
Console.WriteLine("waiting
to finish on thread ...{0}...",
Thread.CurrentThread.ManagedThreadId);
Console.ReadLine();
}
private
static
void
DownloadAynchronously()
{
string[]
urls =
{
"http://www.techcrunch.com/",
"http://www.isohunt.com/",
"http://www.google.com",
"http://www.developerscloud.org"
};
foreach
(var
url in
urls)
{
var
thread = new
Thread(download);
thread.Start(url);
}
}
private
static
void
download(object
url)
{
var
client = new
WebClient();
var
html = client.DownloadString(url.ToString());
Console.WriteLine("download{0}
chars from {1} on thread {2}...",
html.Length,
url, Thread.CurrentThread.ManagedThreadId);
}
}
}
Thanks, I hope you understand the basics of Async Threading.
Comments
Post a Comment
Important - Make sure to click the Notify Me check-box below the comment to be notified of follow up comments and replies.