有关多线程的研究的例子

来源:互联网 发布:java xml转pdf fop 编辑:程序博客网 时间:2024/05/21 13:51
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.  On a uniprocessor, the thread does not get 
        
// any processor time until the main thread 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();
        
    }

}

 运行结果:
 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.

下面就我个人的理解来解释这个结果(完全是个人理解,有错误的地方请指正):

主线程(也即main函数)执行时,首先输出第一行,这应该没什么疑问,然后创建了一个线程t,该线程委托调用的方法是静态方法ThreadProc,t.start()启动该线程,此时该程序中已经有两个线程了,当然首席执行的是主线程,进入到main函数的for循环时,这里时关键了,有点难理解,第一次, i=0,输出一个Main thread: Do some work.然后使主线程阻塞0毫秒,主线程阻塞后就会执行前面创建的那个线程,然后调用ThreadProc方法,输出ThreadProc: 0,然后线程t调用sleep阻塞,回到主线程,i=1,输出Main thread: Do some work,然后主线程又调用sleep阻塞又跳到线程t,输出 ThreadProc: 1,如此重复了4次(即i=3),第四次线程t执行完之后跳回主线程继续执行主线程下面的内容,输出Main thread: Call Join(), to wait until ThreadProc ends.接着调用了t.Join(),就是接着上次线程t执行的地方向下执行,然后一次输出 :ThreadProc: 4
 ThreadProc: 5
 ThreadProc: 6
 ThreadProc: 7
 ThreadProc: 8
 ThreadProc: 9

输出完ThreadProc: 9之后,线程t执行完毕,然后继续执行主线程,输出Main thread: ThreadProc.Join has returned.  Press Enter to end program.

以上就是我分析的结果,我们可以对上面的代码进一步的分析,比如把main函数中的for循环里的sleep时间改为2000,然后把ThreadProc方法中的sleep时间改为1000,再运行代码,观察运行的结果,结果会变为:

Main thread: Start a second thread.
 Main thread: Do some work.
 ThreadProc: 0

ThreadProc: 1
 Main thread: Do some work.
  ThreadProc: 2

ThreadProc: 3
 Main thread: Do some work.
  ThreadProc: 4
 ThreadProc: 5
 Main thread: Do some work.
ThreadProc: 6
 ThreadProc: 7

 Main thread: Call Join(), to wait until ThreadProc ends.
 
 ThreadProc: 8
 ThreadProc: 9
 Main thread: ThreadProc.Join has returned.  Press Enter to end program.

原因就是主线程的阻塞时间是线程t的阻塞时间的两倍,所以每输出一个 Main thread: Do some work.会输出两个ThreadProc: i。

你还可以把t.start下面的那句//Thread.Sleep(0);取消注释,即让主线程还没进入循环之前就阻塞,观察运行结果又是如何?自己研究研究,呵呵,我就不解释了。

原创粉丝点击