Home > Net >  How to calculate the console using multithreading method of run time?
How to calculate the console using multithreading method of run time?

Time:09-22

to calculate multi-threading implementation method of the time, but has been unable to realize, it's my code, a great god advice please help, thank you!

CodePudding user response:

When measuring methods can be placed in doTestThread inside,

CodePudding user response:

DoTestThread method is thread bodies, when complete thread execution completed doTestThread, sw timing doTestThread ~ with thread running time

CodePudding user response:

Is there a way to calculate all the thread from the beginning to the end of the time it takes?

CodePudding user response:

Consider using Task, the Task is a class of thread encapsulation, Task, Wait will Wait for the specified Task performed completely, then can calculate the total execution time,

CodePudding user response:

Note: the Task class is the.net 4.0 introduced, early version does not support,

CodePudding user response:

Mean with wrap the Thread to perform the task?

CodePudding user response:

WaitHandle. WaitAll ()

The key to you, his study

CodePudding user response:

I am a little messed up ~ you always give me a great god can explain the effect of ` I need is simple, execution of multiple threads at the same time, the timing starts from the beginning of that a moment, until the last time the end of the thread, whether also can understand it, in each thread to join the single thread execution time, takes the longest time is all threads execute?

CodePudding user response:

refer to the eighth floor ft1507999 response:
I am a little messed up ~ you always give me a great god can explain the effect of ` I need is simple, execution of multiple threads at the same time, the timing starts from the beginning of that a moment, until the last time the end of the thread, whether also can understand it, in each thread to join the single thread execution time, when is the longest of all threads execute time?


Don't need to explain so much, you have only one word "synchronization", you can use only WaitHandle, even those who said the task, actually still is still WaitHandle

CodePudding user response:

C # wait for all the child thread execution method

CodePudding user response:

 
A Stopwatch Stopwatch=new Stopwatch ();
A stopwatch. Start ();

Task [] tasks=new Task []
{
Task. Factory. StartNew (()=& gt; {
Thread.sleep (1000);
}),
Task. Factory. StartNew (()=& gt; {
for (int i=0; i <2; I++)
{
Thread.sleep (1000);
}
}),
Task. Factory. StartNew (()=& gt; {
for (int i=0; i <3; I++)
{
Thread.sleep (1000);
}
}),
};

Task. WaitAll (tasks);

A stopwatch. Stop ();

Console. WriteLine (stopwatch. Elapsed. TotalSeconds);
The Console. ReadKey ();

CodePudding user response:

refer to the eighth floor ft1507999 response:
I am a little messed up ~ you always give me a great god can explain the effect of ` I need is simple, execution of multiple threads at the same time, the timing starts from the beginning of that a moment, until the last time the end of the thread, whether also can understand it, in each thread to join the single thread execution time, when is the longest of all threads execute time?
I don't think the execution time of the longest, such as 3 thread A, B, C respectively carry out 1 s, 2 s, 3 s, A thread execution after 1 s B, C thread starts, so the total time is 4 s

CodePudding user response:

Make an instance variable, assign a initial time, then start a thread, at the end of each thread method to set the current time, it is presupposed, and the two time is not

CodePudding user response:

refer to the second floor hands suddenly and violently skin reply:
doTestThread method is thread, thread is performed when doTestThread has been completed, sw timing doTestThread is thread running time ~

CodePudding user response:

reference 15 floor the knights templar 18 response:
make an instance variable, assign a initial time, then start a thread, at the end of each thread method to set the current time, it is the two time is presupposed, not yao


It to a problem, assuming that the last method will be at the end of the 24 points and 10 seconds 500 milliseconds, excuse me, how do I know he is over, "schrodinger's cat", opened the know the result is not rare, the trouble is that he is now "I don't know life and death"

CodePudding user response:

refer to 17th floor wanghui0380 response:
Quote: the knights templar 18 references 15 floor response:

Make an instance variable, assign a initial time, then start a thread, at the end of each thread method to set the current time, it is the two time is presupposed, not yao


It to a problem, assuming that the last method will be at the end of the 24 points and 10 seconds 500 milliseconds, excuse me, how do I know he is over, "schrodinger's cat", opened the know the result is not rare, the trouble is that he is now "I don't know life and death"

Can emit an event

CodePudding user response:

Give you write a simple example,

First not a separate program should also be encapsulated as an independent Action object, it'll be easier to each work really serious study, transmission,

After encapsulation, here can use two methods to execute them respectively,

 using System; 
Using System. Linq;
Using System. The Threading;
Using System. The Threading. The Tasks;

The namespace ConsoleApp1
{
Class Program
{
The static void Main (string [] args)
{
Var RND=new Random ();
Var action1=new Action (()=& gt;
{
Thread.sleep (RND. Next (500, 2000));
Console. WriteLine (" complete "Action_1);
});
Var action2=new Action (()=& gt;
{
Thread.sleep (RND. Next (500, 2000));
Console. WriteLine (" complete "Action_2);
});
Var action3=new Action (()=& gt;
{
Thread.sleep (RND. Next (500, 2000));
Console. WriteLine (" complete "Action_3);
});
Var action4=new Action (()=& gt;
{
Thread.sleep (RND. Next (500, 2000));
Console. WriteLine (" complete "Action_4);
});
Var array=new Action [] {action1, action2, action3, action4};
Console. WriteLine (" synchronous perform array: ");
The foreach (var proc in array)
Proc ();
Console. WriteLine (" concurrent execution array: ");
Wait (array);
Console. WriteLine ("... Press any key to end ");
The Console. ReadKey ();
}

The static void Wait (Action [] actions)
{
Var tasks=actions. Select (async proc=& gt;
{
Await Task. The Yield ();
Proc ();
});
Task. WaitAll (tasks. ToArray ());
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related