C # Multithreading meets long time-consuming operations and multitasking

Source: Internet
Author: User

4.0 with Task.Factory.StartNew (() =>{});
4.0 below with ThreadPool.QueueUserWorkItem (() =>{})
More than 4.0 in Task.run (() =>{});

Multi-tasking:

task[] tasks = new Task[maxcurrent];

for (int i = Beginid; I <= maxid; i + = interval, counter++)

Tasks[counter] = new Task (worker. Testhandler, taskcreationoptions.longrunning);


var continuation = Task.Factory.ContinueWhenAll (
Tasks, (antecedents) =>{

Loginfo ("All Threads has loaded!");

});


foreach (Task t in Tasks)

T.start ();


Loginfo ("All threads has been queued. Waiting to complete ... ");

while (!continuation. iscompleted)

Thread.Sleep (1000);

static random _random = new Random ();
static void Main (string[] args)
{
ArrayList listthread = new ArrayList ();
ArrayList Listresult = new ArrayList ();
for (int i = 0; i <; i++)
{
Thread thread = new Thread (new Parameterizedthreadstart (Workthread));
Thread. Start (Listresult);
Listthread.add (thread);
}

foreach (thread thread in Listthread)
{
Thread. Join ();
}

foreach (int i in Listresult)
{
Console.WriteLine (i);
}
}

static void Workthread (Object list)
{
int cnt = _random. Next (1,10);
ArrayList listlocal = new ArrayList ();
for (int i = 0; i < cnt; i++)
{
Listlocal.add (CNT);
Thread.Sleep (100);
}

Lock (List)
{
(list as ArrayList). AddRange (listlocal);
}
}

C # Multithreading meets long time-consuming operations and multitasking

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.