Home > Net >  What is the task. Run run order
What is the task. Run run order

Time:10-01

The namespace ConsoleApp2
{
Class Program
{
The static void Main (string [] args)
{
int a=0;
The Task Task=Task. Run (()=& gt; Dowork (
Ref a));
Console. WriteLine (a);
}
The static void dowork (ref int a)
{
A=2;
Console. WriteLine (a);
}


}

}


Why isn't the output is 0, 2, 2, 2

CodePudding user response:

int a=0;
The Task Task=Task. Run (()=& gt; Dowork (
Ref a));
Console. WriteLine (a);
The main thread after the issue of task, go to execute next Console. WriteLine (a); Whatever task thread, task perform their own

Order:
The static void Main (string [] args)
{
1. int a=0;
2. Task Task=Task. Run (()=& gt; Dowork (
Ref a));
3. Console. WriteLine (a);
}
The static void dowork (ref int a)
{
A=2;
, Console. WriteLine (a);
}

CodePudding user response:

0 is the Main output; 2 consists of Task output
Task execution does not block the main thread
We will strictly control execution order, you need to use the semaphore or other synchronization method

CodePudding user response:

That how to change to output 2

CodePudding user response:

0, 2 and 2, 2, from the design at this is uncertain, can't take it for granted to say "what the output, not output", because the two pieces of code is and !

CodePudding user response:

reference qq_39937801 reply: 3/f
that how modifications to output 2


Write code that is very simple, so the
 static void Main (string [] args) 
{
int a=0;
Dowork (ref a);
Console. WriteLine (a);
}
The static void dowork (ref int a)
{
A=2;
Console. WriteLine (a);
}


Don't own concurrent multi-threaded operation, i.e., again to ask "how to produce is not the result of the simultaneous multi-threading," that is for the sake of the Task and the Task to write code, redundant,

CodePudding user response:

reference qq_39937801 reply: 3/f
that how modifications to output 2

The namespace ConsoleApp2
{
Class Program
{
The static void Main (string [] args)
{
int a=0;
The Task Task=Task. Run (()=& gt; Dowork (
Ref a));
task. Wait ();
Console. WriteLine (a);
}
The static void dowork (ref int a)
{
A=2;
Console. WriteLine (a);
}


}

}

CodePudding user response:

Task allocation from the thread pool will not block the main thread (unless the display block), the code in the order to continue, namely after Task execution is behind does not affect the output of the code, namely "asynchronous", study its usage:
https://msdn.microsoft.com/zh-cn/library/system.threading.tasks.task.aspx to use not only, more want to know what circumstance can use,

CodePudding user response:

You run a task,
So 0 or 2 is possible,
For thread and task is concurrent execution,
As for whether to let task execution output, can wait

CodePudding user response:

When I run several times 2, 0

CodePudding user response:

Not running order, thread based on Windows is he didn't order, or thread wouldn't be so embarrassed novice

Because of their simple open threads any couple would like to open and then open, but the thread synchronization, signal control, control, communication between threads context is the key to thread the novice to use

Otherwise the basic book, also won't thread chapters 3, 4 pages will tell you the thread is what, how to open a thread, and then spend 30 or 40 pages tell you how to thread synchronization, thread scheduling, thread scheduling, communication between threads

The same code also proved, is what needs to be locked, why the need for the cause of the thread security class object, because he was disordered, you can't guarantee that he will run to you want it to

CodePudding user response:

You appear 0, 2 is only depends on the context of the running speed, does not depend on his running order,

Several upstairs wait, it is synchronous, forced to let him run out

And if

int a=0;
The Task Task=Task. Run (()=& gt; Dowork (
Ref a));
for(i=0; I<100; I++)//what do you want to deliberately let more slowly than the thread running outside, so he is 2, 2
{
}
Console. WriteLine (a);

CodePudding user response:

The main thread in any Task, so add 0, 2, you can directly control the Task finished then perform follow-up operations Task. Wait () can be done

CodePudding user response:

Random. Doesn't determine the running order, if you need to run A, B running again,
Use ContinueWith
If you want to finish all run run something again, with whenAll

CodePudding user response:

It's a good question, Task. Run to open a new thread, and the Run started Task does not block the main thread, the main thread in any Task is complete or not in the Run the following Task, therefore, the main method in the Console. WriteLine (a); Is prior to the run of the completion of tasks of the child thread to execute, when the child thread dowork method has not been called, a natural value is 0, 0, so the child thread then performed a=2; The action when a value is to 2,
If you want the output to 2, then need to Wait for the child thread completes the main thread and then perform the following Console. WriteLine (a), the method is a task. The Wait ();
So the main method of code is changed to the following:
int a=0;
The Task Task=Task. Run (()=& gt; Dowork (ref a));
task. Wait ();//wait for the task to execute the following code, without it, so the following code will be executed before the task finished,
Console. WriteLine (a);

Summarize: Task. The method opens up a new threads in the Run, and not specified in the main thread to Wait for it to complete, the main thread is not blocked (so called asynchronous), and in the Task. The Wait (); Method need to explicitly specify the main thread after the child thread process to execute that code, then becomes a synchronization,
Multithreading is, in general, more around, this is the result of computer work principle and profound, I started learning when the asynchronous, synchronous and at the same time always points not clear, the concept of preliminary understanding can be thought of asynchronous is for tasks at the same time, who also vary, and synchronization is when we don't have a multithreaded learning, it is not at the same time, is at the back of the code must be performed to perform in front of the code, such as
  •  Tags:  
  • C#
  • Related