Multi threading on WIndows

我只是一个虾纸丫 提交于 2020-01-15 11:37:27

问题


using System;
using System.Threading;

// Simple threading scenario:  Start a static method running
// on a second thread.
public class ThreadExample {
    // The ThreadProc method is called when the thread starts.
    // It loops ten times, writing to the console and yielding 
    // the rest of its time slice each time, and then ends.
    public static void ThreadProc() {
        for (int i = 0; i < 10; i++) {
            Console.WriteLine("ThreadProc: {0}", i);
            // Yield the rest of the time slice.
            Thread.Sleep(0);
        }
    }

    public static void Main() {
        Console.WriteLine("Main thread: Start a second thread.");
        // The constructor for the Thread class requires a ThreadStart 
        // delegate that represents the method to be executed on the 
        // thread.  C# simplifies the creation of this delegate.
        Thread t = new Thread(new ThreadStart(ThreadProc));

        // Start ThreadProc.  Note that on a uniprocessor, the new 
        // thread does not get any processor time until the main thread 
        // is preempted or yields.  Uncomment the Thread.Sleep that 
        // follows t.Start() to see the difference.
        t.Start();
        //Thread.Sleep(0);

        for (int i = 0; i < 4; i++) {
            Console.WriteLine("Main thread: Do some work.");
            Thread.Sleep(0);
        }

        Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
        t.Join();
        Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.");
        Console.ReadLine();
    }
}

The result is :

 Main thread: Start a second thread.
 Main thread: Do some work.
 ThreadProc: 0
 Main thread: Do some work.
 ThreadProc: 1
 Main thread: Do some work.
 ThreadProc: 2
 Main thread: Do some work.
 ThreadProc: 3
 Main thread: Call Join(), to wait until ThreadProc ends.
 ThreadProc: 4
 ThreadProc: 5
 ThreadProc: 6
 ThreadProc: 7
 ThreadProc: 8
 ThreadProc: 9
 Main thread: ThreadProc.Join has returned.  Press Enter to end program.

I don't understand why "ThreadProc 0" "1" "2" "3" can appear in between " Main thread: Do some work."

Can anybody help me explain it? thanks!


回答1:


The "t.Start()" line starts the other thread which is running simultaneously while the main thread is doing it's work.




回答2:


I think it would help you to read up about threads in general in computer science.

A thread (in my words) is an asynchronous unit of work. Your processor hops between different threads in your program to complete work at different intervals. The benefit you get is being able to perform work on one thread while another is waiting for something (like Thread.Sleep(0)). You also can utilize multiple core CPUs, since one core can execute one thread the same time as another.

Does that explain some?




回答3:


The idea is that each thread is like a mini-process contained within the process of your program. The operating system then divides up the CPU execution time amongst them.

The calls to Sleep hasten the switching from one thread to the next. Without them, you might not see the intertwined output, but then again you might - the point is it's not really up to you when you make multiple threads which one executes and when - that's why you have to assume any thread might execute at any time and leads you into the concepts of locking and synchronization (which I'm sure you'll run into next or soon).




回答4:


This seems correct. As soon as you start ThreadProc

t.Start();

it runs at the same time as main thread. So the System will print whichever print statement happens first. You will get the statements merged because both loops go at the same time.




回答5:


MSDN saying that Thread.Start() method

Causes the operating system to change the state of the current instance to ThreadState.Running. Once a thread is in the ThreadState.Running state, the operating system can schedule it for execution.

Output you've provided clearly shows that OS ran thread immediately and then switches control between both threads, try out

t.Priority = ThreadPriority.Lowest;
t.Start();

and see whether execution order has changed



来源:https://stackoverflow.com/questions/7047290/multi-threading-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!