using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ThreadStart thr_start_func = new ThreadStart(First_Thread);
            Console.WriteLine("Creating the first thread ");
            Thread fThread = new Thread(thr_start_func);
            fThread.Name = "first_thread";
            fThread.Start(); //starting the thread
            Thread sThread = new Thread(new ThreadStart(Second_Thread));
            sThread.Name = "second_thread";
            sThread.Start();
        }        public static void First_Thread()
        {
            Console.WriteLine("First thread created");
            Thread current_thread = Thread.CurrentThread;
            string thread_details = "Thread Name: " + current_thread.Name + "\r\nThread State: " + current_thread.ThreadState.ToString() + "\r\n Thread Priority level:" + current_thread.Priority.ToString();
            Console.WriteLine("The details of the thread are :" + thread_details);
            Console.WriteLine("first thread terminated");
        }        public static void Second_Thread()
        {
            Console.WriteLine("Second thread created");
            Thread current_thread = Thread.CurrentThread;
            string thread_details = "Thread Name: " + current_thread.Name + "\r\nThread State: " + current_thread.ThreadState.ToString() + "\r\n Thread Priority level:" + current_thread.Priority.ToString();
            Console.WriteLine("The details of the thread are :" + thread_details);
            Console.WriteLine("second thread terminated");
        }
    }
}
//如果不是,谁能提供一个简单的、多线明确的源代码示例?

解决方案 »

  1.   

    也算
    http://www.cnblogs.com/greatandforever/archive/2008/07/08/1238300.html
      

  2.   

    问题继续:线程的并发,我的理解就是线程的同步问题。我在网上看到的资料说,线程同步容易产生的两个问题:1、死锁;2、脏数据;我的理解是这两个问题的解决,应该是通过管理解决的。听有经验的人讲,多线程中,存在主线程。那在帖子的这段代码中,启动first和second的main是不是主线程?另外,解决脏数据的方法是对公共数据操作部分加锁,如果要实现加锁,是不是也应该在主线程中做?
      

  3.   

    线程之间确实是并发执行的,虽然启动有先后,不过启动后线程之间就是互相独立的,不能确定谁先结束,谁先访问某个资源。另外现在的cpu都是单任务的,就是一个时间帧内cpu只能执行一段指令,至于进程线程间并发执行的基础是基于操作系统的多任务抢占式处理机制,一个时间片内执行这个任务的一段指令,下一个时间片又执行另一个任务的指令,这样交替进行,给人造成一种计算机在同时处理着多个任务的感觉。产生死锁和脏数据也是因为这样的资源竞争机制,通过对锁粒度的控制可以降低死锁的几率,但是只要存在资源竞争和并发,死锁就是无法完全避免的,只是有经验的开发人员能将发生的几率降到可以控制的程度。所有进程都有一个主线程,Main函数就是这个主线程的入口点。主线程结束意味着一个进程的结束,该进程的其它线程都回被杀掉